Skip to content

Commit

Permalink
Merge pull request #180 from swissquote/autohome
Browse files Browse the repository at this point in the history
Automatically find Maven Home
  • Loading branch information
sonthanh authored Feb 8, 2024
2 parents 0c8e443 + 52001b0 commit 22ba2b1
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 8 deletions.
6 changes: 0 additions & 6 deletions .github/workflows/java.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<CarnotzetModuleCoordinates, String> moduleNameProvider;

private final Path resourcesPath;
Expand All @@ -42,6 +45,16 @@ public class MavenDependencyResolver {
private final ConcurrentHashMap<CarnotzetModuleCoordinates, Node> dependencyTreeCache = new ConcurrentHashMap<>();
private Path localRepoPath;

public MavenDependencyResolver(Function<CarnotzetModuleCoordinates, String> moduleNameProvider, Path resourcesPath) {
this.moduleNameProvider = moduleNameProvider;
this.resourcesPath = resourcesPath;

File mavenHome = getMavenHome();
if (mavenHome != null) {
maven.setMavenHome(mavenHome);
}
}

public List<CarnotzetModule> resolve(CarnotzetModuleCoordinates topLevelModuleId, Boolean failOnCycle) {
log.debug("Resolving module dependencies");
Node tree = getDependenciesTree(topLevelModuleId);
Expand Down Expand Up @@ -177,6 +190,42 @@ private void executeMavenBuild(List<String> 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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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<CarnotzetModuleCoordinates, String> 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<CarnotzetModule> modules = resolver.resolve(coord, true);

assertEquals(modules.size(), 6);
}
}

0 comments on commit 22ba2b1

Please sign in to comment.