[MNG-8502] Embedded executor should obey MAVEN_ARGS env variable (#2032)

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.

---

https://issues.apache.org/jira/browse/MNG-8502
This commit is contained in:
Tamas Cservenak 2025-01-08 17:01:07 +01:00 committed by GitHub
parent 99452bb868
commit 209cd7fbe4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 33 additions and 14 deletions

View File

@ -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;
@ -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;
@ -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;
@ -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);
@ -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);
@ -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()) {

View File

@ -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
@ -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
@ -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
@ -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);
}
}