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 e441ebb1cd7d756792878ac18563f14a9132da9d
Author: Sylwester Lachiewicz <[email protected]>
AuthorDate: Thu Nov 5 01:09:39 2026 +0000

    Keep system properties in MAVEN_OPTS and quote tokens with whitespace
---
 .../org/apache/maven/executor/ExecutorRequest.java |  9 +++--
 .../maven/executor/forked/ForkedMavenExecutor.java | 13 ++++---
 .../executor/forked/ForkedMavenExecutorTest.java   | 43 +++++++++++++++-------
 3 files changed, 42 insertions(+), 23 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 adeb23f..b58a070 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
@@ -75,10 +75,11 @@ 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.
+     * executors pass them as {@code -Dkey=value} through {@code MAVEN_OPTS}, 
so they are system properties of the
+     * forked JVM too. A value that contains whitespace is double-quoted, 
which {@code mvn.cmd} and the Maven 4
+     * {@code mvn} script honor; the Maven 3 Unix {@code mvn} script 
word-splits {@code MAVEN_OPTS}, so there such a
+     * value breaks the command line. The Maven 4 script evaluates {@code 
MAVEN_OPTS}, so {@code $}, backticks and
+     * {@code "} in a value are interpreted by the shell.
      *
      * @return an Optional containing the map of Java System Properties, 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 0897595..c87da78 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
@@ -112,11 +112,6 @@ 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<>();
@@ -126,6 +121,12 @@ public class ForkedMavenExecutor extends 
ProcessBuilderExecutorSupport implement
         if (executorRequest.jvmArguments().isPresent()) {
             jvmArgs.addAll(executorRequest.jvmArguments().get());
         }
+        if (executorRequest.jvmSystemProperties().isPresent()) {
+            executorRequest
+                    .jvmSystemProperties()
+                    .get()
+                    .forEach((k, v) -> jvmArgs.add(quoteWhitespace("-D" + k + 
"=" + v)));
+        }
 
         HashMap<String, String> env = new HashMap<>();
         if (executorRequest.environmentVariables().isPresent()) {
@@ -161,6 +162,8 @@ public class ForkedMavenExecutor extends 
ProcessBuilderExecutorSupport implement
      * 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.
+     * Characters that a shell treats specially ({@code $}, backticks, {@code 
"}) are not escaped: the Maven 4 script
+     * interprets them when it evaluates MAVEN_OPTS.
      */
     private static String quoteWhitespace(String token) {
         for (int i = 0; i < token.length(); i++) {
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 a8d48ca..3326c16 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
@@ -21,7 +21,7 @@ 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 java.util.Locale;
 
 import org.apache.maven.executor.Environment;
 import org.apache.maven.executor.Executor;
@@ -32,6 +32,7 @@ import org.junit.jupiter.api.Test;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assumptions.assumeTrue;
 
 /**
  * Forked executor UT
@@ -43,21 +44,35 @@ public class ForkedMavenExecutorTest extends 
MavenExecutorTestSupport {
         return new ForkedMavenExecutor(installationDirectory);
     }
 
+    /** Maven 4 script evaluates MAVEN_OPTS, so the quoted token survives. */
     @Test
-    void systemPropertyValueWithSpaces() throws Exception {
+    void systemPropertyValueWithSpaces4() throws Exception {
+        assertSystemPropertyWithSpaces(Environment.MAVEN4_HOME);
+    }
+
+    /**
+     * The Maven 3 Unix {@code mvn} script word-splits MAVEN_OPTS, so a value 
with whitespace cannot be passed there;
+     * only {@code mvn.cmd} honors the quotes.
+     */
+    @Test
+    void systemPropertyValueWithSpaces3() throws Exception {
+        assumeTrue(
+                
System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("windows"),
+                "the Maven 3 Unix mvn script word-splits MAVEN_OPTS");
+        assertSystemPropertyWithSpaces(Environment.MAVEN3_HOME);
+    }
+
+    private void assertSystemPropertyWithSpaces(String mavenHome) 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);
-        }
+        ExecutorResult result = createAndMemoizeExecutor(Paths.get(mavenHome))
+                .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);
     }
 
     /**

Reply via email to