From 209cd7fbe4d81218cc6b8ba2c72845afc19a5cbd Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Wed, 8 Jan 2025 17:01:07 +0100 Subject: [PATCH] [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 --- .../embedded/EmbeddedMavenExecutor.java | 23 ++++++++++++++---- .../cling/executor/impl/HelperImplTest.java | 24 +++++++++++-------- 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/impl/maven-executor/src/main/java/org/apache/maven/cling/executor/embedded/EmbeddedMavenExecutor.java b/impl/maven-executor/src/main/java/org/apache/maven/cling/executor/embedded/EmbeddedMavenExecutor.java index e713a73a43ad..76928b124717 100644 --- a/impl/maven-executor/src/main/java/org/apache/maven/cling/executor/embedded/EmbeddedMavenExecutor.java +++ b/impl/maven-executor/src/main/java/org/apache/maven/cling/executor/embedded/EmbeddedMavenExecutor.java @@ -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 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 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 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 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()) { diff --git a/impl/maven-executor/src/test/java/org/apache/maven/cling/executor/impl/HelperImplTest.java b/impl/maven-executor/src/test/java/org/apache/maven/cling/executor/impl/HelperImplTest.java index aa209350870f..d0ddf73f413e 100644 --- a/impl/maven-executor/src/test/java/org/apache/maven/cling/executor/impl/HelperImplTest.java +++ b/impl/maven-executor/src/test/java/org/apache/maven/cling/executor/impl/HelperImplTest.java @@ -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); } }