Skip to content

Commit

Permalink
For Maven, if a dependency with runtime and test, a runtime dependenc…
Browse files Browse the repository at this point in the history
…y is added (#2683)
  • Loading branch information
sdelamo authored Jan 7, 2025
1 parent 113fbcc commit a3d5e86
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,20 @@ public boolean isAnnotationProcessorPriority() {
return annotationProcessorPriority;
}

public static Dependency of(Dependency dep, Scope scope) {
return new Dependency(scope,
dep.getGroupId(),
dep.getArtifactId(),
dep.getVersion(),
dep.getVersionProperty(),
dep.requiresLookup(),
dep.isAnnotationProcessorPriority(),
dep.getOrder(),
dep.isPom(),
dep.getExclusions(),
dep.getSubstitutions());
}

public static class Builder {

private Scope scope;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@
import io.micronaut.starter.options.BuildTool;
import io.micronaut.starter.options.Language;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.function.Predicate;

import static io.micronaut.starter.build.dependencies.Phase.COMPILATION;
Expand Down Expand Up @@ -79,8 +75,10 @@ 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);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ class FeatureWithDuplicatesSpec extends BeanContextSpec implements CommandOutput
verifier.hasDependency("org.gebish", "geb-core", Scope.COMPILE)

and: 'for a dependency in runtime and test, both runtime and test should be added'
verifier.hasDependency("org.seleniumhq.selenium", "selenium-firefox-driver", Scope.RUNTIME)
verifier.hasDependency("org.seleniumhq.selenium", "selenium-firefox-driver", Scope.TEST)
if (buildTool.isGradle()) {
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.RUNTIME)
}

where:
[language, buildTool] << [Language.values(), BuildTool.values()].combinations()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package io.micronaut.starter.build.maven

import io.micronaut.starter.BeanContextSpec
import io.micronaut.starter.application.generator.DependencyContextImpl
import io.micronaut.starter.build.dependencies.CoordinateResolver
import io.micronaut.starter.build.dependencies.Dependency
import io.micronaut.starter.build.dependencies.DependencyContext
import io.micronaut.starter.build.dependencies.Scope
import io.micronaut.starter.fixture.CommandOutputFixture
import io.micronaut.starter.options.BuildTool
import io.micronaut.starter.options.Language

class MavenDependencySpec extends BeanContextSpec implements CommandOutputFixture {

public static final String ARTIFACT_ID_LOGBACK_CLASSIC = "logback-classic"
public static final String GROUP_ID_LOGBACK = "ch.qos.logback"

void "maven dependency in test and runtime should be only in compile"() {
given:
CoordinateResolver coordinateResolver = beanContext.getBean(CoordinateResolver)
DependencyContext dependencyContext = new DependencyContextImpl(coordinateResolver)

when:
List<Dependency> dependencies = dependencyContext.removeDuplicates(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(),
), Language.JAVA, BuildTool.MAVEN)

then:
dependencies == List.of(
Dependency.builder().scope(Scope.COMPILE).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.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.RUNTIME).groupId(GROUP_ID_LOGBACK).artifactId(ARTIFACT_ID_LOGBACK_CLASSIC).build())

when:
dependencies = dependencyContext.removeDuplicates(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(),
), Language.JAVA, BuildTool.MAVEN)

then:
dependencies == List.of(
Dependency.builder().scope(Scope.COMPILE).groupId(GROUP_ID_LOGBACK).artifactId(ARTIFACT_ID_LOGBACK_CLASSIC).build())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package io.micronaut.starter.feature.logging

import io.micronaut.context.annotation.Requires
import io.micronaut.starter.ApplicationContextSpec
import io.micronaut.starter.BuildBuilder
import io.micronaut.starter.application.ApplicationType
import io.micronaut.starter.application.generator.GeneratorContext
import io.micronaut.starter.build.BuildTestUtil
import io.micronaut.starter.build.BuildTestVerifier
import io.micronaut.starter.build.dependencies.Dependency
import io.micronaut.starter.build.dependencies.Scope
import io.micronaut.starter.feature.Feature
import io.micronaut.starter.fixture.CommandOutputFixture
import io.micronaut.starter.options.BuildTool
import io.micronaut.starter.options.Language
import jakarta.inject.Singleton

class LogbackInTestAndRuntimeSpec extends ApplicationContextSpec implements CommandOutputFixture {
private static final String ARTIFACT_ID_LOGBACK_CLASSIC = "logback-classic"
private static final String GROUP_ID_LOGBACK = "ch.qos.logback"

Map<String, Object> getConfiguration() {
return super.getConfiguration() + ["spec.name": "LogbackInTestAndRuntimeSpec"]
}

void "compile if runtime and test dependency"() {
given:
BuildTool buildTool = BuildTool.MAVEN
Language language = Language.JAVA
when:
String template = new BuildBuilder(beanContext, buildTool)
.language(language)
.features(["test-logback"])
.render()
BuildTestVerifier verifier = BuildTestUtil.verifier(buildTool, language, template)

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

@Requires(property = "spec.name", value = "LogbackInTestAndRuntimeSpec")
@Singleton
static class TestLogback implements Feature {

@Override
String getName() {
return "test-logback"
}

@Override
String getTitle() {
return "Test Logback"
}

@Override
void apply(GeneratorContext generatorContext) {
generatorContext.addDependency(Dependency.builder().groupId(GROUP_ID_LOGBACK).artifactId(ARTIFACT_ID_LOGBACK_CLASSIC).scope(Scope.TEST).build())
}

@Override
boolean supports(ApplicationType applicationType) {
true
}
}
}

0 comments on commit a3d5e86

Please sign in to comment.