Skip to content

Commit

Permalink
Executor improvements
Browse files Browse the repository at this point in the history
Salvaged from the "split repo for ITs" experiment. The experiment failed
but this improvement is good to have, as makes the embedded executor
behave more closely like forked executor.

Embedded so far did not obey MAVEN_ARGS env variable while
forked did.
  • Loading branch information
cstamas committed Jan 8, 2025
1 parent b99eac7 commit 87839e5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -108,6 +109,7 @@ public int hashCode() {
}

protected final boolean cacheContexts;
protected final boolean useMavenArgsEnv;
protected final AtomicBoolean closed;
protected final PrintStream originalStdout;
protected final PrintStream originalStderr;
Expand All @@ -116,11 +118,12 @@ public int hashCode() {
protected final ConcurrentHashMap<Key, Context> contexts;

public EmbeddedMavenExecutor() {
this(true);
this(true, true);
}

public EmbeddedMavenExecutor(boolean cacheContexts) {
public EmbeddedMavenExecutor(boolean cacheContexts, boolean useMavenArgsEnv) {
this.cacheContexts = cacheContexts;
this.useMavenArgsEnv = useMavenArgsEnv;
this.closed = new AtomicBoolean(false);
this.originalStdout = System.out;
this.originalStderr = System.err;
Expand Down Expand Up @@ -223,6 +226,14 @@ protected Context doCreate(Path mavenHome, ExecutorRequest executorRequest) {
"Installation directory does not point to Maven installation: " + mavenHome);
}

ArrayList<String> mavenArgs = new ArrayList<>();
String mavenArgsEnv = System.getenv("MAVEN_ARGS");
if (useMavenArgsEnv && mavenArgsEnv != null && !mavenArgsEnv.isEmpty()) {
Arrays.stream(mavenArgsEnv.split(" "))
.filter(s -> !s.trim().isEmpty())
.forEach(s -> mavenArgs.add(0, s));
}

Properties properties = prepareProperties(executorRequest);

System.setProperties(properties);
Expand Down Expand Up @@ -264,8 +275,10 @@ protected Context doCreate(Path mavenHome, ExecutorRequest executorRequest) {
exec = r -> {
System.setProperties(prepareProperties(r));
try {
ArrayList<String> args = new ArrayList<>(mavenArgs);
args.addAll(r.arguments());
return (int) doMain.invoke(mavenCli, new Object[] {
r.arguments().toArray(new String[0]), r.cwd().toString(), null, null
args.toArray(new String[0]), r.cwd().toString(), null, null
});
} catch (Exception e) {
throw new ExecutorException("Failed to execute", e);
Expand All @@ -285,7 +298,9 @@ protected Context doCreate(Path mavenHome, ExecutorRequest executorRequest) {
|| r.stderrConsumer().isPresent()) {
ansiConsoleInstalled.set(null, 1);
}
return (int) mainMethod.invoke(null, r.arguments().toArray(new String[0]), classWorld);
ArrayList<String> args = new ArrayList<>(mavenArgs);
args.addAll(r.arguments());
return (int) mainMethod.invoke(null, args.toArray(new String[0]), classWorld);
} finally {
if (r.stdoutConsumer().isPresent()
|| r.stderrConsumer().isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,11 @@ void artifactPath3(ExecutorHelper.Mode mode) {
EMBEDDED_MAVEN_EXECUTOR,
FORKED_MAVEN_EXECUTOR);
String path = helper.artifactPath(helper.executorRequest(), "aopalliance:aopalliance:1.0", "central");
assertEquals(
"aopalliance" + File.separator + "aopalliance" + File.separator + "1.0" + File.separator
+ "aopalliance-1.0.jar",
path);
// split repository: assert "ends with" as split may introduce prefixes
assertTrue(
path.endsWith("aopalliance" + File.separator + "aopalliance" + File.separator + "1.0" + File.separator
+ "aopalliance-1.0.jar"),
"path=" + path);
}

@ParameterizedTest
Expand All @@ -148,10 +149,11 @@ void artifactPath4(ExecutorHelper.Mode mode) {
EMBEDDED_MAVEN_EXECUTOR,
FORKED_MAVEN_EXECUTOR);
String path = helper.artifactPath(helper.executorRequest(), "aopalliance:aopalliance:1.0", "central");
assertEquals(
"aopalliance" + File.separator + "aopalliance" + File.separator + "1.0" + File.separator
+ "aopalliance-1.0.jar",
path);
// split repository: assert "ends with" as split may introduce prefixes
assertTrue(
path.endsWith("aopalliance" + File.separator + "aopalliance" + File.separator + "1.0" + File.separator
+ "aopalliance-1.0.jar"),
"path=" + path);
}

@ParameterizedTest
Expand All @@ -164,7 +166,8 @@ void metadataPath3(ExecutorHelper.Mode mode) {
EMBEDDED_MAVEN_EXECUTOR,
FORKED_MAVEN_EXECUTOR);
String path = helper.metadataPath(helper.executorRequest(), "aopalliance", "someremote");
assertEquals("aopalliance" + File.separator + "maven-metadata-someremote.xml", path);
// split repository: assert "ends with" as split may introduce prefixes
assertTrue(path.endsWith("aopalliance" + File.separator + "maven-metadata-someremote.xml"), "path=" + path);
}

@ParameterizedTest
Expand All @@ -177,6 +180,7 @@ void metadataPath4(ExecutorHelper.Mode mode) {
EMBEDDED_MAVEN_EXECUTOR,
FORKED_MAVEN_EXECUTOR);
String path = helper.metadataPath(helper.executorRequest(), "aopalliance", "someremote");
assertEquals("aopalliance" + File.separator + "maven-metadata-someremote.xml", path);
// split repository: assert "ends with" as split may introduce prefixes
assertTrue(path.endsWith("aopalliance" + File.separator + "maven-metadata-someremote.xml"), "path=" + path);
}
}

0 comments on commit 87839e5

Please sign in to comment.