diff --git a/.github/workflows/java.yml b/.github/workflows/java.yml index afa6a02..5a0624e 100644 --- a/.github/workflows/java.yml +++ b/.github/workflows/java.yml @@ -23,13 +23,7 @@ jobs: java-version: ${{ matrix.java-version }} distribution: 'zulu' cache: maven - - name: Maven Info - run: echo "home=$(mvn -v -q | grep "Maven home:" | sed -e 's/Maven home://' -e 's/^[[:space:]]*//')" >> $GITHUB_OUTPUT - shell: bash - id: maven - run: mvn --batch-mode install - env: - M2_HOME: '${{ steps.maven.outputs.home }}' - name: Test Report uses: dorny/test-reporter@v1 if: success() || failure() diff --git a/core/src/main/java/com/github/swissquote/carnotzet/core/maven/MavenDependencyResolver.java b/core/src/main/java/com/github/swissquote/carnotzet/core/maven/MavenDependencyResolver.java index 3d7f9a2..b245ef0 100644 --- a/core/src/main/java/com/github/swissquote/carnotzet/core/maven/MavenDependencyResolver.java +++ b/core/src/main/java/com/github/swissquote/carnotzet/core/maven/MavenDependencyResolver.java @@ -1,5 +1,6 @@ package com.github.swissquote.carnotzet.core.maven; +import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; @@ -24,15 +25,17 @@ import com.github.swissquote.carnotzet.core.CarnotzetDefinitionException; import com.github.swissquote.carnotzet.core.CarnotzetModule; +import com.github.swissquote.carnotzet.core.runtime.DefaultCommandRunner; import lombok.Getter; -import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @Slf4j -@RequiredArgsConstructor public class MavenDependencyResolver { + private static final String MAVEN_HOME_PROPERTY = "maven.home"; + private static final String M2_HOME = "M2_HOME"; + private final Function moduleNameProvider; private final Path resourcesPath; @@ -42,6 +45,16 @@ public class MavenDependencyResolver { private final ConcurrentHashMap dependencyTreeCache = new ConcurrentHashMap<>(); private Path localRepoPath; + public MavenDependencyResolver(Function moduleNameProvider, Path resourcesPath) { + this.moduleNameProvider = moduleNameProvider; + this.resourcesPath = resourcesPath; + + File mavenHome = getMavenHome(); + if (mavenHome != null) { + maven.setMavenHome(mavenHome); + } + } + public List resolve(CarnotzetModuleCoordinates topLevelModuleId, Boolean failOnCycle) { log.debug("Resolving module dependencies"); Node tree = getDependenciesTree(topLevelModuleId); @@ -177,6 +190,42 @@ private void executeMavenBuild(List goals, InvocationOutputHandler outpu } } + private static boolean isBlank(String str) { + return str == null || str.trim().isEmpty(); + } + + private static File getMavenHome() { + if (!isBlank(System.getProperty(MAVEN_HOME_PROPERTY))) { + File mavenHome = new File(System.getProperty(MAVEN_HOME_PROPERTY)); + if (mavenHome.exists()) { + return mavenHome; + } + } + if (!isBlank(System.getenv(M2_HOME))) { + File mavenHome = new File(System.getenv(M2_HOME)); + if (mavenHome.exists()) { + return mavenHome; + } + } + + // Get it directly from maven + String out = DefaultCommandRunner.INSTANCE.runCommandAndCaptureOutput("mvn", "help:evaluate", "-Dexpression=maven.home"); + String[] lines = out.split("\\R"); + for (String line : lines) { + if (line.contains("INFO")) { + continue; + } + + File mavenHome = new File(line.trim()); + if (mavenHome.exists()) { + return mavenHome; + } + } + + log.warn("Could not find a maven home in {} or {} or by calling mvn", MAVEN_HOME_PROPERTY, M2_HOME); + return null; + } + /** * Relies on mvn dependency:tree -Dverbose to get a full dependency tree, including omitted ones. * This uses maven 2 and may be inconsistent with maven 3 resolved dependencies. diff --git a/core/src/test/java/com/github/swissquote/carnotzet/core/maven/MavenDependencyResolverTest.java b/core/src/test/java/com/github/swissquote/carnotzet/core/maven/MavenDependencyResolverTest.java new file mode 100644 index 0000000..86832c0 --- /dev/null +++ b/core/src/test/java/com/github/swissquote/carnotzet/core/maven/MavenDependencyResolverTest.java @@ -0,0 +1,43 @@ +package com.github.swissquote.carnotzet.core.maven; + +import static org.junit.Assert.assertEquals; + +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.List; +import java.util.function.Function; +import java.util.regex.Pattern; + +import org.junit.Ignore; +import org.junit.Test; + +import com.github.swissquote.carnotzet.core.Carnotzet; +import com.github.swissquote.carnotzet.core.CarnotzetConfig; +import com.github.swissquote.carnotzet.core.CarnotzetModule; + +public class MavenDependencyResolverTest { + + private Function getDefaultModuleResolver() { + Pattern moduleFilterPattern = Pattern.compile(CarnotzetConfig.DEFAULT_MODULE_FILTER_PATTERN); + Pattern classifierIncludePattern = Pattern.compile(CarnotzetConfig.DEFAULT_CLASSIFIER_INCLUDE_PATTERN); + + return (module) -> Carnotzet.getModuleName(module, moduleFilterPattern, classifierIncludePattern); + } + + @Test + @Ignore("Dependencies are not published to Maven Central which makes it impossible to run this test in CI for now") + public void resolveDependencies() { + Path resourcesPath = Paths.get("/tmp/carnotzet_" + System.nanoTime()); + + MavenDependencyResolver resolver = new MavenDependencyResolver( + getDefaultModuleResolver(), + resourcesPath.resolve("maven") + ); + + CarnotzetModuleCoordinates coord = new CarnotzetModuleCoordinates("com.github.swissquote.examples", "voting-all-carnotzet", "1.8.9-SNAPSHOT", null); + + List modules = resolver.resolve(coord, true); + + assertEquals(modules.size(), 6); + } +}