Skip to content

Commit bd08070

Browse files
authored
✨ feat(lockfile): Add resolve and repo id field to external poms (#1478)
1 parent bb881fe commit bd08070

File tree

4 files changed

+66
-2
lines changed

4 files changed

+66
-2
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import io.github.chains_project.maven_lockfile.data.MavenPlugin;
1111
import io.github.chains_project.maven_lockfile.data.MetaData;
1212
import io.github.chains_project.maven_lockfile.data.Pom;
13+
import io.github.chains_project.maven_lockfile.data.RepositoryId;
14+
import io.github.chains_project.maven_lockfile.data.ResolvedUrl;
1315
import io.github.chains_project.maven_lockfile.data.VersionNumber;
1416
import io.github.chains_project.maven_lockfile.graph.DependencyGraph;
1517
import io.github.chains_project.maven_lockfile.reporting.PluginLogManager;
@@ -367,7 +369,10 @@ private static Pom constructRecursivePom(
367369
.relativize(project.getFile().toPath())
368370
.toString();
369371
String checksum = null;
372+
ResolvedUrl resolved = null;
373+
RepositoryId repoId = null;
370374
if (project.getFile() == null) {
375+
// External POM - get repository information
371376
Artifact artifact = project.getArtifact();
372377
Artifact pomArtifact = new DefaultArtifact(
373378
artifact.getGroupId(),
@@ -378,6 +383,9 @@ private static Pom constructRecursivePom(
378383
artifact.getClassifier(),
379384
artifact.getArtifactHandler());
380385
checksum = checksumCalculator.calculateArtifactChecksum(pomArtifact);
386+
RepositoryInformation repoInfo = checksumCalculator.getArtifactResolvedField(pomArtifact);
387+
resolved = repoInfo.getResolvedUrl();
388+
repoId = repoInfo.getRepositoryId();
381389
} else {
382390
checksum = checksumCalculator.calculatePomChecksum(
383391
project.getFile().toPath());
@@ -387,6 +395,8 @@ private static Pom constructRecursivePom(
387395
ArtifactId.of(project.getArtifactId()),
388396
VersionNumber.of(project.getVersion()),
389397
relativePath,
398+
resolved,
399+
repoId,
390400
checksumAlgorithm,
391401
checksum,
392402
lastPom);

maven_plugin/src/main/java/io/github/chains_project/maven_lockfile/data/Pom.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ public class Pom implements Comparable<Pom> {
66
private final ArtifactId artifactId;
77
private final VersionNumber version;
88
private final String relativePath;
9+
private final ResolvedUrl resolved;
10+
private final RepositoryId repositoryId;
911
private final String checksumAlgorithm;
1012
private final String checksum;
1113
private final Pom parent;
@@ -15,13 +17,17 @@ public Pom(
1517
ArtifactId artifactId,
1618
VersionNumber version,
1719
String relativePath,
20+
ResolvedUrl resolved,
21+
RepositoryId repositoryId,
1822
String checksumAlgorithm,
1923
String checksum,
2024
Pom parent) {
2125
this.groupId = groupId;
2226
this.artifactId = artifactId;
2327
this.version = version;
2428
this.relativePath = relativePath;
29+
this.resolved = resolved;
30+
this.repositoryId = repositoryId;
2531
this.checksumAlgorithm = checksumAlgorithm;
2632
this.checksum = checksum;
2733
this.parent = parent;
@@ -43,6 +49,14 @@ public String getRelativePath() {
4349
return relativePath;
4450
}
4551

52+
public ResolvedUrl getResolved() {
53+
return resolved;
54+
}
55+
56+
public RepositoryId getRepositoryId() {
57+
return repositoryId;
58+
}
59+
4660
public String getChecksumAlgorithm() {
4761
return checksumAlgorithm;
4862
}
@@ -76,6 +90,20 @@ public int compareTo(Pom o) {
7690
return pathCmp.compareTo(oPathCmp);
7791
}
7892

93+
ResolvedUrl resolvedCmp = this.resolved == null ? ResolvedUrl.Unresolved() : this.resolved;
94+
ResolvedUrl oResolvedCmp = o.resolved == null ? ResolvedUrl.Unresolved() : o.resolved;
95+
96+
if (resolvedCmp.compareTo(oResolvedCmp) != 0) {
97+
return resolvedCmp.compareTo(oResolvedCmp);
98+
}
99+
100+
RepositoryId repoIdCmp = this.repositoryId == null ? RepositoryId.None() : this.repositoryId;
101+
RepositoryId oRepoIdCmp = o.repositoryId == null ? RepositoryId.None() : o.repositoryId;
102+
103+
if (repoIdCmp.compareTo(oRepoIdCmp) != 0) {
104+
return repoIdCmp.compareTo(oRepoIdCmp);
105+
}
106+
79107
if (this.checksumAlgorithm.compareTo(o.checksumAlgorithm) != 0) {
80108
return this.checksumAlgorithm.compareTo(o.checksumAlgorithm);
81109
}
@@ -108,16 +136,27 @@ public boolean equals(Object obj) {
108136
return false;
109137
}
110138
Pom other = (Pom) obj;
139+
// Poms are either defined by their relative path or resolved from a repository by their GAV.
140+
// We cannot know where poms defined by their relative path will be hosted and thus their
141+
// resolved fields are null.
111142
String pathCmp = this.relativePath == null ? "" : this.relativePath;
112143
String otherPathCmp = other.relativePath == null ? "" : other.relativePath;
113144

145+
ResolvedUrl resolvedCmp = this.resolved == null ? ResolvedUrl.Unresolved() : this.resolved;
146+
ResolvedUrl otherResolvedCmp = other.resolved == null ? ResolvedUrl.Unresolved() : other.resolved;
147+
148+
RepositoryId repoIdCmp = this.repositoryId == null ? RepositoryId.None() : this.repositoryId;
149+
RepositoryId otherRepoIdCmp = other.repositoryId == null ? RepositoryId.None() : other.repositoryId;
150+
114151
boolean parentEqual = (this.parent == null && other.parent == null)
115152
|| (this.parent != null && other.parent != null && this.parent.equals(other.parent));
116153

117154
return this.groupId.equals(other.groupId)
118155
&& this.artifactId.equals(other.artifactId)
119156
&& this.version.equals(other.version)
120157
&& pathCmp.equals(otherPathCmp)
158+
&& resolvedCmp.equals(otherResolvedCmp)
159+
&& repoIdCmp.equals(otherRepoIdCmp)
121160
&& this.checksumAlgorithm.equals(other.checksumAlgorithm)
122161
&& this.checksum.equals(other.checksum)
123162
&& parentEqual;

maven_plugin/src/test/java/io/github/chains_project/maven_lockfile/graph/LockfileTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ void shouldLockFilesEqualWhenOrderIsChanged() {
4242
groupId,
4343
artifactId,
4444
version,
45-
new Pom(groupId, artifactId, version, "pom.xml", "SHA-256", "POM-CHECKSUM", null),
45+
new Pom(groupId, artifactId, version, "pom.xml", null, null, "SHA-256", "POM-CHECKSUM", null),
4646
Set.of(dependencyNodeA(dependencyNodeAChild1(), dependencyNodeAChild2()), dependencyNodeB()),
4747
Set.of(pluginA(), pluginB()),
4848
metadata);
@@ -51,7 +51,7 @@ void shouldLockFilesEqualWhenOrderIsChanged() {
5151
groupId,
5252
artifactId,
5353
version,
54-
new Pom(groupId, artifactId, version, "pom.xml", "SHA-256", "POM-CHECKSUM", null),
54+
new Pom(groupId, artifactId, version, "pom.xml", null, null, "SHA-256", "POM-CHECKSUM", null),
5555
Set.of(dependencyNodeB(), dependencyNodeA(dependencyNodeAChild1(), dependencyNodeAChild2())),
5656
Set.of(pluginB(), pluginA()),
5757
metadata);

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,12 @@ public void externalParentPom(MavenExecutionResult result) throws Exception {
568568
assertThat(parentPom.getChecksum()).isNotBlank();
569569
// External pom should not have a relativePath
570570
assertThat(parentPom.getRelativePath()).isNull();
571+
// External pom should have resolved URL and repositoryId
572+
assertThat(parentPom.getResolved()).isNotNull();
573+
assertThat(parentPom.getResolved().getValue()).contains("spring-boot-starter-parent");
574+
assertThat(parentPom.getResolved().getValue()).endsWith(".pom");
575+
assertThat(parentPom.getRepositoryId()).isNotNull();
576+
assertThat(parentPom.getRepositoryId().getValue()).isNotBlank();
571577

572578
// Verify grandparent pom is present (Spring Boot dependencies)
573579
var grandparentPom = parentPom.getParent();
@@ -577,6 +583,12 @@ public void externalParentPom(MavenExecutionResult result) throws Exception {
577583
.extracting(ArtifactId::getValue)
578584
.isEqualTo("spring-boot-dependencies");
579585
assertThat(grandparentPom.getChecksum()).isNotBlank();
586+
// External grandparent pom should also have resolved URL and repositoryId
587+
assertThat(grandparentPom.getResolved()).isNotNull();
588+
assertThat(grandparentPom.getResolved().getValue()).contains("spring-boot-dependencies");
589+
assertThat(grandparentPom.getResolved().getValue()).endsWith(".pom");
590+
assertThat(grandparentPom.getRepositoryId()).isNotNull();
591+
assertThat(grandparentPom.getRepositoryId().getValue()).isNotBlank();
580592
}
581593

582594
@MavenTest
@@ -606,5 +618,8 @@ public void relativeParentPom(MavenExecutionResult result) throws Exception {
606618
.isEqualTo("relative-parent-pom-parent-project");
607619
assertThat(parentPom.getChecksum()).isNotBlank();
608620
assertThat(parentPom.getRelativePath()).isEqualTo("../pom.xml");
621+
// Local parent pom should NOT have resolved URL or repositoryId
622+
assertThat(parentPom.getResolved()).isNull();
623+
assertThat(parentPom.getRepositoryId()).isNull();
609624
}
610625
}

0 commit comments

Comments
 (0)