Skip to content

Commit

Permalink
if maven runtime and test - > runtime
Browse files Browse the repository at this point in the history
in graadle if runtime and test => runtime and test
  • Loading branch information
sdelamo committed Jan 7, 2025
1 parent 25d7361 commit b82bb85
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,46 +75,18 @@ default List<Dependency> removeDuplicates(Collection<Dependency> dependencies, L
dependenciesInTestClasspathWithoutDuplicates.removeIf(testDep -> {
MavenCoordinate test = new MavenCoordinate(testDep.getGroupId(), testDep.getArtifactId(), testDep.getVersion());
return dependenciesInMainClasspathWithoutDuplicates.stream()
.filter(mainDep -> (mainDep.getScope().getPhases().contains(RUNTIME) && mainDep.getScope().getPhases().contains(COMPILATION)))
.anyMatch(mainDep -> {
.filter(mainDep ->
(buildTool == BuildTool.MAVEN && mainDep.getScope().getPhases().contains(RUNTIME)) ||
(mainDep.getScope().getPhases().contains(RUNTIME) && mainDep.getScope().getPhases().contains(COMPILATION))
).anyMatch(mainDep -> {
MavenCoordinate main = new MavenCoordinate(mainDep.getGroupId(), mainDep.getArtifactId(), mainDep.getVersion());
return main.equals(test);
});
});
List<Dependency> result = new ArrayList<>(dependenciesNotInMainOrTestClasspath);
result.addAll(dependenciesInMainClasspathWithoutDuplicates);
result.addAll(dependenciesInTestClasspathWithoutDuplicates);
result = result.stream().sorted(Dependency.COMPARATOR).toList();
return filteredMavenRuntimeTestDependencies(result, buildTool);
}

private static List<Dependency> filteredMavenRuntimeTestDependencies(List<Dependency> dependencies, BuildTool buildTool) {
if (buildTool.isGradle()) {
return dependencies;
}
List<Dependency> result = new ArrayList<>();

Set<MavenCoordinate> coordinatesAdded = new HashSet<>();
for (Dependency d : dependencies) {
MavenCoordinate coord = new MavenCoordinate(d.getGroupId(), d.getArtifactId(), d.getVersion());
if (d.getScope() == Scope.RUNTIME &&
dependencies.stream().anyMatch(dep ->
dep.getScope() == Scope.TEST &&
dep.getGroupId().equals(d.getGroupId()) &&
dep.getArtifactId().equals(d.getArtifactId()))) {
result.add(Dependency.of(d, Scope.COMPILE));
coordinatesAdded.add(coord);
} else if (d.getScope() == Scope.TEST &&
d.getGroupId().equals(d.getGroupId()) &&
d.getArtifactId().equals(d.getArtifactId())) {
if (!coordinatesAdded.contains(coord)) {
result.add(d);
}
} else {
result.add(d);
}
}
return result;
return result.stream().sorted(Dependency.COMPARATOR).toList();
}

private static List<Dependency> filterDuplicates(List<Dependency> dependencies) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class FeatureWithDuplicatesSpec extends BeanContextSpec implements CommandOutput
assert verifier.hasDependency("org.seleniumhq.selenium", "selenium-firefox-driver", Scope.RUNTIME)
assert verifier.hasDependency("org.seleniumhq.selenium", "selenium-firefox-driver", Scope.TEST)
} else if(buildTool == BuildTool.MAVEN) {
assert verifier.hasDependency("org.seleniumhq.selenium", "selenium-firefox-driver", Scope.COMPILE)
assert verifier.hasDependency("org.seleniumhq.selenium", "selenium-firefox-driver", Scope.RUNTIME)
}

where:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,27 @@ class MavenDependencySpec extends BeanContextSpec implements CommandOutputFixtu
dependencies == List.of(
Dependency.builder().scope(Scope.COMPILE).groupId(GROUP_ID_LOGBACK).artifactId(ARTIFACT_ID_LOGBACK_CLASSIC).build())

dependencyContext.removeDuplicates(List.of(
when:
dependencies = dependencyContext.removeDuplicates(List.of(
Dependency.builder().scope(Scope.TEST).groupId(GROUP_ID_LOGBACK).artifactId(ARTIFACT_ID_LOGBACK_CLASSIC).build(),
Dependency.builder().scope(Scope.RUNTIME).groupId(GROUP_ID_LOGBACK).artifactId(ARTIFACT_ID_LOGBACK_CLASSIC).build(),
), Language.JAVA, BuildTool.GRADLE)

then:
dependencies == List.of(
Dependency.builder().scope(Scope.RUNTIME).groupId(GROUP_ID_LOGBACK).artifactId(ARTIFACT_ID_LOGBACK_CLASSIC).build(),
Dependency.builder().scope(Scope.TEST).groupId(GROUP_ID_LOGBACK).artifactId(ARTIFACT_ID_LOGBACK_CLASSIC).build(),
)

when:
dependencies = dependencyContext.removeDuplicates(List.of(
Dependency.builder().scope(Scope.TEST).groupId(GROUP_ID_LOGBACK).artifactId(ARTIFACT_ID_LOGBACK_CLASSIC).build(),
Dependency.builder().scope(Scope.RUNTIME).groupId(GROUP_ID_LOGBACK).artifactId(ARTIFACT_ID_LOGBACK_CLASSIC).build(),
), Language.JAVA, BuildTool.MAVEN)

then:
dependencies == List.of(
Dependency.builder().scope(Scope.COMPILE).groupId(GROUP_ID_LOGBACK).artifactId(ARTIFACT_ID_LOGBACK_CLASSIC).build())
Dependency.builder().scope(Scope.RUNTIME).groupId(GROUP_ID_LOGBACK).artifactId(ARTIFACT_ID_LOGBACK_CLASSIC).build())

when:
dependencies = dependencyContext.removeDuplicates(List.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ class LogbackInTestAndRuntimeSpec extends ApplicationContextSpec implements Com
BuildTestVerifier verifier = BuildTestUtil.verifier(buildTool, language, template)

then:
verifier.hasDependency(GROUP_ID_LOGBACK, ARTIFACT_ID_LOGBACK_CLASSIC, Scope.COMPILE)
!verifier.hasDependency(GROUP_ID_LOGBACK, ARTIFACT_ID_LOGBACK_CLASSIC, Scope.RUNTIME)
verifier.hasDependency(GROUP_ID_LOGBACK, ARTIFACT_ID_LOGBACK_CLASSIC, Scope.RUNTIME)
!verifier.hasDependency(GROUP_ID_LOGBACK, ARTIFACT_ID_LOGBACK_CLASSIC, Scope.TEST)
}

Expand Down

0 comments on commit b82bb85

Please sign in to comment.