Skip to content

Commit 1287d48

Browse files
feat: order lockfile entries by groupId, artifactId and version (#890)
1 parent 22b7c1d commit 1287d48

File tree

6 files changed

+85
-5
lines changed

6 files changed

+85
-5
lines changed

maven_plugin/src/main/java/io/github/chains_project/maven_lockfile/LockFileFacade.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public static LockFile generateLockFileFromProject(
9999
var roots = graph.getGraph().stream()
100100
.filter(v -> v.getParent() == null)
101101
.collect(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(
102-
io.github.chains_project.maven_lockfile.graph.DependencyNode::getChecksum))));
102+
io.github.chains_project.maven_lockfile.graph.DependencyNode::getComparatorString))));
103103
return new LockFile(
104104
GroupId.of(project.getGroupId()),
105105
ArtifactId.of(project.getArtifactId()),

maven_plugin/src/main/java/io/github/chains_project/maven_lockfile/graph/DependencyGraph.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public Set<DependencyNode> getRoots() {
2020
return graph.stream()
2121
.filter(node -> node.getParent() == null)
2222
.collect(Collectors.toCollection(
23-
() -> new TreeSet<>(Comparator.comparing(DependencyNode::getChecksum))));
23+
() -> new TreeSet<>(Comparator.comparing(DependencyNode::getComparatorString))));
2424
}
2525

2626
private DependencyGraph(Set<DependencyNode> graph) {
@@ -60,15 +60,15 @@ public static DependencyGraph of(
6060
var roots = graph.nodes().stream()
6161
.filter(it -> graph.predecessors(it).isEmpty())
6262
.collect(Collectors.toList());
63-
Set<DependencyNode> nodes = new TreeSet<>(Comparator.comparing(DependencyNode::getChecksum));
63+
Set<DependencyNode> nodes = new TreeSet<>(Comparator.comparing(DependencyNode::getComparatorString));
6464
for (var artifact : roots) {
6565
createDependencyNode(artifact, graph, calc, true, reduced).ifPresent(nodes::add);
6666
}
6767
// maven dependency tree contains the project itself as a root node. We remove it here.
6868
Set<DependencyNode> dependencyRoots = nodes.stream()
6969
.flatMap(v -> v.getChildren().stream())
7070
.collect(Collectors.toCollection(
71-
() -> new TreeSet<>(Comparator.comparing(DependencyNode::getChecksum))));
71+
() -> new TreeSet<>(Comparator.comparing(DependencyNode::getComparatorString))));
7272
dependencyRoots.forEach(v -> v.setParent(null));
7373
return new DependencyGraph(dependencyRoots);
7474
}

maven_plugin/src/main/java/io/github/chains_project/maven_lockfile/graph/DependencyNode.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public class DependencyNode implements Comparable<DependencyNode> {
6161
this.classifier = classifier;
6262
this.checksumAlgorithm = checksumAlgorithm;
6363
this.checksum = checksum;
64-
this.children = new TreeSet<>(Comparator.comparing(DependencyNode::getChecksum));
64+
this.children = new TreeSet<>(Comparator.comparing(DependencyNode::getComparatorString));
6565
this.id = new NodeId(groupId, artifactId, version);
6666
this.scope = scope;
6767
}
@@ -231,4 +231,9 @@ public String toString() {
231231
+ ", scope=" + scope + ", selectedVersion=" + selectedVersion + ", id=" + id + ", parent=" + parent
232232
+ ", children=" + children + "]";
233233
}
234+
235+
public String getComparatorString() {
236+
return this.getGroupId().getValue() + ":" + this.getArtifactId().getValue() + "@"
237+
+ this.getVersion().getValue() + "-" + this.getChecksum();
238+
}
234239
}

maven_plugin/src/test/java/it/IntegrationTestsIT.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static com.soebes.itf.extension.assertj.MavenITAssertions.assertThat;
44
import static org.junit.jupiter.api.Assertions.assertTrue;
55

6+
import com.google.common.collect.Ordering;
67
import com.soebes.itf.jupiter.extension.MavenJupiterExtension;
78
import com.soebes.itf.jupiter.extension.MavenTest;
89
import com.soebes.itf.jupiter.maven.MavenExecutionResult;
@@ -19,6 +20,7 @@
1920
import java.nio.file.Path;
2021
import java.util.ArrayList;
2122
import java.util.List;
23+
import java.util.stream.Collectors;
2224
import org.apache.commons.io.FileUtils;
2325
import org.apache.maven.model.Dependency;
2426
import org.apache.maven.model.Model;
@@ -315,4 +317,18 @@ public void withoutEnvironmentFromLockfile(MavenExecutionResult result) throws E
315317
assertThat(lockFile.getConfig().isIncludeEnvironment()).isFalse();
316318
assertThat(lockFile.getEnvironment()).isNull();
317319
}
320+
321+
@MavenTest
322+
public void orderedLockfile(MavenExecutionResult result) throws Exception {
323+
// contract: the dependency list should be ordered
324+
assertThat(result).isSuccessful();
325+
Path lockFilePath = findFile(result, "lockfile.json");
326+
assertThat(lockFilePath).exists();
327+
var lockFile = LockFile.readLockFile(lockFilePath);
328+
var dependencyList = lockFile.getDependencies().stream()
329+
.map(it -> it.getComparatorString())
330+
.collect(Collectors.toList());
331+
boolean sorted = Ordering.natural().isOrdered(dependencyList);
332+
assertThat(sorted).isTrue();
333+
}
318334
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<project>
2+
<modelVersion>4.0.0</modelVersion>
3+
4+
<groupId>com.mycompany.app</groupId>
5+
<artifactId>my-app</artifactId>
6+
<packaging>jar</packaging>
7+
<version>1</version>
8+
<properties>
9+
<maven.compiler.target>11</maven.compiler.target>
10+
<maven.compiler.source>11</maven.compiler.source>
11+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
12+
</properties>
13+
<dependencies>
14+
<dependency>
15+
<groupId>org.junit.jupiter</groupId>
16+
<artifactId>junit-jupiter-api</artifactId>
17+
<version>5.9.2</version>
18+
</dependency>
19+
<dependency>
20+
<groupId>org.junit.jupiter</groupId>
21+
<artifactId>junit-jupiter-api</artifactId>
22+
<version>5.9.2</version>
23+
<classifier>sources</classifier>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.junit.jupiter</groupId>
27+
<artifactId>junit-jupiter-api</artifactId>
28+
<version>5.9.2</version>
29+
<classifier>javadoc</classifier>
30+
</dependency>
31+
</dependencies>
32+
<build>
33+
<plugins>
34+
<plugin>
35+
<groupId>io.github.chains-project</groupId>
36+
<artifactId>
37+
maven-lockfile</artifactId>
38+
<version>@project.version@</version>
39+
<executions>
40+
<execution>
41+
<goals>
42+
<goal>generate</goal>
43+
</goals>
44+
</execution>
45+
</executions>
46+
<configuration>
47+
<includeEnvironment>false</includeEnvironment>
48+
</configuration>
49+
</plugin>
50+
</plugins>
51+
</build>
52+
</project>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package simpleProject.src.main.java;
2+
3+
public class HelloWorld {
4+
public static void main(String[] args) {
5+
System.out.println("Hello World!");
6+
}
7+
}

0 commit comments

Comments
 (0)