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

Automatically find Maven Home #180

Merged
merged 1 commit into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
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
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);
sonthanh marked this conversation as resolved.
Show resolved Hide resolved

List<CarnotzetModule> modules = resolver.resolve(coord, true);

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