Skip to content

Commit

Permalink
Consolidate maven versions of classifiers for an artifact (#1179)
Browse files Browse the repository at this point in the history
  • Loading branch information
cheister authored Jul 1, 2024
1 parent 9feb3d6 commit eb1adad
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -402,12 +402,61 @@ private List<Dependency> createManagedDependencies(
.filter(dep -> !artifactKeys.contains(getArtifactKey(dep.getArtifact())))
.forEach(managedDependencies::add);

return managedDependencies.build();
// Last, normalize versions across different classifiers of an artifact
// The rules_jvm_external json version file only supports one version for an artifact even when
// the artifact has multiple classifiers e.g.
//
// "artifacts": {
// "io.netty:netty-tcnative-boringssl-static": {
// "shasums": {
// "jar": "b6f974972c44cd6f9cecabc255290286faac40b6393c66c3c3c0db7f421cc28e",
// "linux-aarch_64": "3614395218ae379cec22ccaa089c4f27b9329a660e0d53c93e7cb12b7a2cee46",
// "linux-x86_64": "4ff9d14f1ec6ccee35b78f53a6f3d9c7c54535aa2a76138311c2f619c5e150e1",
// "osx-aarch_64": "58e0302c9fde3db984c3ff7ee7ec7159dc0320bdb91533cc290e12e40911cd1a",
// "osx-x86_64": "fcfea887f4f0802d363c699b444d504b7109a7cb198ae6845eeff63745e5b0ba",
// "windows-x86_64": "17cd2fa3c63b7ed23edea01c945e55cb7baed1faa0f553732c3f5f56da90b3e0"
// },
// "version": "2.0.61.Final"
// },
//
// In order to keep the version consistent across all classifiers we use the first version for
// the
// artifact in the dependency list as the version for all of the classifiers of the artifact.
//
// Note: This is different from the coursier resolver which will use the last artifact in the
// dependency list for the version of all of that dependency with classifiers.
Map<String, String> dependencyVersions = new HashMap<>();
return managedDependencies.build().stream()
.map(
dependency -> {
Artifact artifact = dependency.getArtifact();
String groupIdArtifactId = artifact.getGroupId() + ":" + artifact.getArtifactId();
if (dependencyVersions.containsKey(groupIdArtifactId)) {
Artifact artifactWithNewVersion =
new DefaultArtifact(
artifact.getGroupId(),
artifact.getArtifactId(),
artifact.getClassifier(),
artifact.getExtension(),
dependencyVersions.get(groupIdArtifactId));
return new Dependency(
artifactWithNewVersion,
dependency.getScope(),
dependency.getOptional(),
dependency.getExclusions());
} else {
if (artifact.getVersion() != null && !artifact.getVersion().isEmpty()) {
dependencyVersions.put(groupIdArtifactId, artifact.getVersion());
}
return dependency;
}
})
.collect(ImmutableList.toImmutableList());
}

private static String getArtifactKey(Artifact artifact) {
// Note: We're not using MavenCoordinates.asCoordinates(artifact).asKey() because these
// dependencies are inputs to the maven resolver, so we don't want to change the classifer
// dependencies are inputs to the maven resolver, so we don't want to change the classifier
// or extension before sending them to the resolver.
return new Coordinates(
artifact.getGroupId(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,53 @@ public void shouldPriortizedVersionsfromBomFilesInOrder() {
resolved.nodes());
}

@Test
public void shouldCosolidateDifferentClassifierVersionsForADependency() {
Coordinates nettyCoords = new Coordinates("io.netty:netty-tcnative-boringssl-static");

Coordinates nettyOsxBom =
new Coordinates("io.netty:netty-tcnative-boringssl-static:osx-aarch_64:2.0.47.Final");
Dependency dependencyOsx = new Dependency();
dependencyOsx.setGroupId(nettyCoords.getGroupId());
dependencyOsx.setArtifactId(nettyCoords.getArtifactId());
dependencyOsx.setClassifier("osx-aarch_64");
dependencyOsx.setVersion("2.0.47.Final");
DependencyManagement managedDepsOsx = new DependencyManagement();
managedDepsOsx.addDependency(dependencyOsx);
Model modelOsx = createModel(nettyOsxBom);
modelOsx.setPackaging("pom");
modelOsx.setDependencyManagement(managedDepsOsx);

Coordinates nettyBom = new Coordinates("io.netty:netty-tcnative-boringssl-static:2.0.60.Final");
Dependency dependencyNetty = new Dependency();
dependencyNetty.setGroupId(nettyCoords.getGroupId());
dependencyNetty.setArtifactId(nettyCoords.getArtifactId());
dependencyNetty.setVersion("2.0.60.Final");
DependencyManagement managedDepsNetty = new DependencyManagement();
managedDepsNetty.addDependency(dependencyNetty);
Model modelNetty = createModel(nettyBom);
modelNetty.setPackaging("pom");
modelNetty.setDependencyManagement(managedDepsNetty);

Coordinates coordinates = new Coordinates("com.example:bomclassifiertest:1.0.0");
Model model = createModel(coordinates);
Dependency dependency = new Dependency();
dependency.setGroupId(nettyCoords.getGroupId());
dependency.setArtifactId(nettyCoords.getArtifactId());
model.addDependency(dependency);

Path repo = MavenRepo.create().add(modelOsx).add(modelNetty).add(model, coordinates).getPath();

ResolutionRequest request = prepareRequestFor(repo.toUri(), nettyCoords);
request.addBom(nettyOsxBom);
request.addBom(nettyBom);

Graph<Coordinates> resolved = resolver.resolve(request).getResolution();
assertEquals(
Set.of(new Coordinates("io.netty:netty-tcnative-boringssl-static:2.0.47.Final")),
resolved.nodes());
}

protected Model createModel(Coordinates coords) {
Model model = new Model();
model.setModelVersion("4.0.0");
Expand Down

0 comments on commit eb1adad

Please sign in to comment.