This is an automated email from the ASF dual-hosted git repository. slachiewicz pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/maven-executor.git
commit ecc6f8ec43cc24258754d73adcd48e72f6ccf8b7 Author: Sylwester Lachiewicz <[email protected]> AuthorDate: Tue Nov 3 23:16:08 2026 +0000 Pass system properties to forked Maven on the command line The launcher scripts split MAVEN_OPTS on whitespace, so a value with a space became extra JVM arguments. Fixes #78 --- .../org/apache/maven/executor/ExecutorRequest.java | 9 ++++- .../maven/executor/forked/ForkedMavenExecutor.java | 27 +++++++++---- .../executor/forked/ForkedMavenExecutorTest.java | 46 ++++++++++++++++++++++ 3 files changed, 74 insertions(+), 8 deletions(-) diff --git a/maven-executor/src/main/java/org/apache/maven/executor/ExecutorRequest.java b/maven-executor/src/main/java/org/apache/maven/executor/ExecutorRequest.java index e5a54dd..adeb23f 100644 --- a/maven-executor/src/main/java/org/apache/maven/executor/ExecutorRequest.java +++ b/maven-executor/src/main/java/org/apache/maven/executor/ExecutorRequest.java @@ -73,6 +73,12 @@ public interface ExecutorRequest { /** * Returns the map of Java System Properties to set before executing process. + * <p> + * Embedded executors set them as system properties of this JVM for the duration of the execution. Forked + * executors pass them to Maven as {@code -Dkey=value} command line arguments, which Maven applies as user + * properties and system properties of the forked JVM; this way values may contain whitespace. A property that the + * JVM or the launcher must see at startup, or that Maven 4 already finds among the JVM system properties (such as + * {@code user.home} or {@code java.io.tmpdir}), has to be passed through {@link #jvmArguments()} instead. * * @return an Optional containing the map of Java System Properties, or empty if not specified */ @@ -89,7 +95,8 @@ public interface ExecutorRequest { /** * Returns the list of extra JVM arguments to be passed to the forked process. * These arguments allow for customization of the JVM environment in which tool will run. - * This property is used ONLY by executors that spawn a new JVM. + * This property is used ONLY by executors that spawn a new JVM, which pass them through {@code MAVEN_OPTS}; the + * Maven 3 {@code mvn} script splits that variable on whitespace, so an argument must not contain any. * * @return an Optional containing the list of extra JVM arguments, or empty if not specified */ diff --git a/maven-executor/src/main/java/org/apache/maven/executor/forked/ForkedMavenExecutor.java b/maven-executor/src/main/java/org/apache/maven/executor/forked/ForkedMavenExecutor.java index bd4cfe0..0897595 100644 --- a/maven-executor/src/main/java/org/apache/maven/executor/forked/ForkedMavenExecutor.java +++ b/maven-executor/src/main/java/org/apache/maven/executor/forked/ForkedMavenExecutor.java @@ -26,7 +26,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.NoSuchElementException; -import java.util.stream.Collectors; import org.apache.maven.executor.Executor; import org.apache.maven.executor.ExecutorException; @@ -113,20 +112,20 @@ public class ForkedMavenExecutor extends ProcessBuilderExecutorSupport implement .forEach(cmdAndArguments::add); } + // system properties go on the command line: the launcher scripts split MAVEN_OPTS on whitespace, a command + // line argument is passed to the process as is; they come before the request arguments, so an explicit -D wins + if (executorRequest.jvmSystemProperties().isPresent()) { + executorRequest.jvmSystemProperties().get().forEach((k, v) -> cmdAndArguments.add("-D" + k + "=" + v)); + } cmdAndArguments.addAll(executorRequest.arguments()); ArrayList<String> jvmArgs = new ArrayList<>(); if (!executorRequest.userHomeDirectory().equals(getCanonicalPath(Paths.get(System.getProperty("user.home"))))) { - jvmArgs.add("-Duser.home=" + executorRequest.userHomeDirectory().toString()); + jvmArgs.add(quoteWhitespace("-Duser.home=" + executorRequest.userHomeDirectory())); } if (executorRequest.jvmArguments().isPresent()) { jvmArgs.addAll(executorRequest.jvmArguments().get()); } - if (executorRequest.jvmSystemProperties().isPresent()) { - jvmArgs.addAll(executorRequest.jvmSystemProperties().get().entrySet().stream() - .map(e -> "-D" + e.getKey() + "=" + e.getValue()) - .collect(Collectors.toList())); - } HashMap<String, String> env = new HashMap<>(); if (executorRequest.environmentVariables().isPresent()) { @@ -157,4 +156,18 @@ public class ForkedMavenExecutor extends ProcessBuilderExecutorSupport implement } return doExecuteProcess(executorRequest, pb); } + + /** + * Wraps a MAVEN_OPTS token that contains whitespace in double quotes. The quotes are honored by {@code mvn.cmd} + * (the Java launcher parses the command line) and by the Maven 4 {@code mvn} script (which evaluates MAVEN_OPTS); + * the Maven 3 {@code mvn} script only word-splits MAVEN_OPTS, so there a token cannot contain whitespace at all. + */ + private static String quoteWhitespace(String token) { + for (int i = 0; i < token.length(); i++) { + if (Character.isWhitespace(token.charAt(i))) { + return "\"" + token + "\""; + } + } + return token; + } } diff --git a/maven-executor/src/test/java/org/apache/maven/executor/forked/ForkedMavenExecutorTest.java b/maven-executor/src/test/java/org/apache/maven/executor/forked/ForkedMavenExecutorTest.java index 28928f6..a8d48ca 100644 --- a/maven-executor/src/test/java/org/apache/maven/executor/forked/ForkedMavenExecutorTest.java +++ b/maven-executor/src/test/java/org/apache/maven/executor/forked/ForkedMavenExecutorTest.java @@ -18,10 +18,20 @@ */ package org.apache.maven.executor.forked; +import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; +import org.apache.maven.executor.Environment; import org.apache.maven.executor.Executor; +import org.apache.maven.executor.ExecutorRequest; +import org.apache.maven.executor.ExecutorResult; import org.apache.maven.executor.MavenExecutorTestSupport; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * Forked executor UT @@ -32,4 +42,40 @@ public class ForkedMavenExecutorTest extends MavenExecutorTestSupport { protected Executor doSelectExecutor(Path installationDirectory) { return new ForkedMavenExecutor(installationDirectory); } + + @Test + void systemPropertyValueWithSpaces() throws Exception { + layDownFiles(customizedRequest().build().cwd()); + for (String home : Arrays.asList(Environment.MAVEN3_HOME, Environment.MAVEN4_HOME)) { + ExecutorResult result = createAndMemoizeExecutor(Paths.get(home)) + .execute(customizedRequest() + .jvmSystemProperty("executor.p", "value with spaces") + .argument("help:evaluate") + .argument("-Dexpression=executor.p") + .build()); + String output = + result.stdOutString().orElse("") + result.stdErrString().orElse(""); + assertEquals(0, result.exitCode().orElse(-1), output); + assertTrue(output.contains("value with spaces"), output); + } + } + + /** + * Maven 4 only: its {@code mvn} script evaluates MAVEN_OPTS, so a quoted token survives; the Maven 3 script + * word-splits it, and there a user home with a space cannot be passed. + */ + @Test + void userHomeWithSpaces4() throws Exception { + ExecutorRequest.Builder builder = customizedRequest(); + Path userHome = builder.build().userHomeDirectory().resolveSibling("home with space"); + Files.createDirectories(userHome); + ExecutorResult result = createAndMemoizeExecutor(Paths.get(Environment.MAVEN4_HOME)) + .execute(builder.userHomeDirectory(userHome) + .argument("eu.maveniverse.maven.plugins:toolbox:" + Environment.TOOLBOX_VERSION + ":gav-dump") + .build()); + String output = result.stdOutString().orElse("") + result.stdErrString().orElse(""); + assertEquals(0, result.exitCode().orElse(-1), output); + assertTrue( + output.contains(userHome.resolve(".m2").resolve("settings.xml").toString()), output); + } }
