Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly enforce external BOM constraints in ProjectDependencyResolver #338

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.graph.Dependency;
import org.eclipse.aether.graph.DependencyNode;
import org.eclipse.aether.graph.Exclusion;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.resolution.ArtifactDescriptorResult;
import org.eclipse.aether.util.graph.manager.DependencyManagerUtils;
Expand Down Expand Up @@ -784,15 +785,20 @@ private DependencyNode collectDependencies(ArtifactCoords coords, List<Dependenc
if (descr.getManagedDependencies().isEmpty()) {
constraints = managedDeps;
} else {
constraints = new ArrayList<>(managedDeps.size());
constraints.addAll(managedDeps);
for (var d : descr.getManagedDependencies()) {
var art = d.getArtifact();
if (!map.containsKey(
ArtifactKey.of(art.getGroupId(), art.getArtifactId(), art.getClassifier(), art.getExtension()))) {
constraints.add(d);
var artKey = ArtifactKey.of(art.getGroupId(), art.getArtifactId(), art.getClassifier(), art.getExtension());
var constraint = map.get(artKey);
if (constraint == null) {
map.put(artKey, d);
} else {
var merged = merge(constraint, d);
if (merged != d) {
map.put(artKey, merged);
}
}
}
constraints = new ArrayList<>(map.values());
}
final List<Dependency> directDeps = new ArrayList<>(descr.getDependencies().size());
for (var d : descr.getDependencies()) {
Expand All @@ -803,11 +809,16 @@ private DependencyNode collectDependencies(ArtifactCoords coords, List<Dependenc
var da = d.getArtifact();
var constraint = map
.get(ArtifactKey.of(da.getGroupId(), da.getArtifactId(), da.getClassifier(), da.getExtension()));
directDeps.add(constraint == null ? d : constraint);
if (constraint == null) {
directDeps.add(d);
} else if (d.getExclusions().isEmpty()) {
directDeps.add(constraint);
} else {
directDeps.add(merge(constraint, d));
}
}
var aggregatedRepos = resolver.aggregateRepositories(resolver.getRepositories(),
resolver.newResolutionRepositories(descr.getRepositories()));

root = resolver.getSystem().collectDependencies(resolver.getSession(),
MavenArtifactResolver.newCollectRequest(descr.getArtifact(), directDeps, constraints, List.of(),
aggregatedRepos))
Expand All @@ -827,6 +838,35 @@ private DependencyNode collectDependencies(ArtifactCoords coords, List<Dependenc
return root;
}

private static Dependency merge(Dependency dominant, Dependency recessive) {
if (dominant == null) {
return recessive;
}
if (recessive.getExclusions().isEmpty()) {
return dominant;
}
if (dominant.getArtifact().getVersion().equals(recessive.getArtifact().getVersion())) {
if (dominant.getExclusions().isEmpty()) {
return recessive;
}
return new Dependency(dominant.getArtifact(), recessive.getScope(), recessive.isOptional(),
mergeExclusions(dominant, recessive));
}
if (dominant.getExclusions().isEmpty()) {
return new Dependency(dominant.getArtifact(), recessive.getScope(), recessive.isOptional(),
recessive.getExclusions());
}
return new Dependency(dominant.getArtifact(), recessive.getScope(), recessive.isOptional(),
mergeExclusions(dominant, recessive));
}

private static List<Exclusion> mergeExclusions(Dependency dominant, Dependency recessive) {
var merged = new ArrayList<Exclusion>(dominant.getExclusions().size() + recessive.getExclusions().size());
merged.addAll(dominant.getExclusions());
merged.addAll(recessive.getExclusions());
return merged;
}

private boolean isCollectNonManagedVisited() {
return config.isLogSummary() && config.isIncludeNonManaged() || config.isLogNonManagedVisitied();
}
Expand Down
Loading