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

Deduplicate replaced dependencies #259

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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 @@ -345,7 +345,9 @@ private Project buildProjectHelper(MavenProject mavenProject, Artifact artifact,
Dependency dep = new Dependency();
dep.setProject(child);
dep.setScope(translateScope(mavenDependency.getScope()));
dependencies.add(dep);
// Although this new dependency is generally unique, the replacement mechanism may introduce a duplicate
// dependency if several original artifacts are replaced with the same final artifact.
addDependencyResolveDuplicate(dep, dependencies); // ensures we don't introduce duplicates (important for J2CL)
}
project.setDependencies(dependencies);

Expand All @@ -372,6 +374,23 @@ private Project buildProjectHelper(MavenProject mavenProject, Artifact artifact,
return project;
}

private static void addDependencyResolveDuplicate(Dependency newDep, List<Dependency> dependencies) {
// We check if there is already an existing dependency in the list with the same project
for (int i = 0; i < dependencies.size(); i++) {
Dependency existingDep = dependencies.get(i);
if (Objects.equals(newDep.getProject(), existingDep.getProject())) {
// If we found one, we must keep only 1 dependency. The 2 can differ only by the scope, which has only 2
// possible values here: COMPILE or BOTH. BOTH is the one to keep in case of duplicates.
if (newDep.getScope() == Dependency.Scope.BOTH) {
dependencies.set(i, newDep);
}
return;
}
}
// If we reach this point, it means there was no existing dependency with the same project in the list
dependencies.add(newDep); // So we can add it
}

private MavenProject resolveNonReactorProjectForArtifact(ProjectBuilder projectBuilder, ProjectBuildingRequest request, Artifact mavenDependency) throws ProjectBuildingException {
MavenProject p;
request.setProject(null);
Expand Down