This is an automated email from the ASF dual-hosted git repository.

gnodet pushed a commit to branch maven-4.0.x
in repository https://gitbox.apache.org/repos/asf/maven.git


The following commit(s) were added to refs/heads/maven-4.0.x by this push:
     new 611041feaf fix(cli): preserve quoted empty arguments in settings and 
JVM configuration
611041feaf is described below

commit 611041feaf0d5467a0647caca7c5816277c9b62b
Author: Guillaume Nodet <[email protected]>
AuthorDate: Thu Nov 19 13:37:18 2026 +0100

    fix(cli): preserve quoted empty arguments in settings and JVM configuration
    
    fix(cli): preserve quoted empty arguments in settings and JVM configuration
    
    - Track whether a token has started independently of its length so that
      `""` and `''` produce an empty argument token instead of being silently
      dropped; inline `<options>`/`<alias>` and structured `<arg/>` forms now
      produce identical argument lists
    - Generalise the backslash escape rules: outside quotes backslashes are
      literal; inside double-quoted strings only `\"` and `\\` are recognised
      as escapes, all other backslashes stay literal; unquoted
      backslash-space sequences no longer combine words into one argument
      (migration: use `"-Dfoo=hello world"` instead of `-Dfoo=hello\ world`)
    - Preserve unquoted Windows paths including `-javaagent:D:\a\...` and
      UNC paths without modification
    - Throw `IllegalArgumentException` with a clear message on unclosed
      single or double quotes, caught in `main()` and reported as a
      configuration error so Maven exits cleanly instead of launching with
      wrong options
    - Keep the standalone `JvmConfigParser` algorithm in sync with the
      settings `ArgumentTokenizer`; remove unused CLI wrapper
    - Expand project-directory placeholders after tokenisation so substituted
      spaces and Windows backslashes remain literal
    - Backported to `maven-4.0.x` (#14076): same empty-argument and
      unclosed-quote fixes; backslash-escape semantics change is intentionally
      excluded (unquoted backslashes continue to act as escapes on 4.0.x with
      a deprecation warning to migrate before 4.2.0)
---
 apache-maven/pom.xml                               |  5 ++
 .../src/assembly/maven/bin/JvmConfigParser.java    | 32 +++++---
 .../src/test/java/JvmConfigParserTest.java         | 90 ++++++++++++++++++++++
 3 files changed, 116 insertions(+), 11 deletions(-)

diff --git a/apache-maven/pom.xml b/apache-maven/pom.xml
index 73e821d2da..b33da3aac7 100644
--- a/apache-maven/pom.xml
+++ b/apache-maven/pom.xml
@@ -33,6 +33,11 @@ under the License.
   <description>The Apache Maven distribution, source and binary, in zip and 
tar.gz formats.</description>
 
   <dependencies>
+    <dependency>
+      <groupId>org.junit.jupiter</groupId>
+      <artifactId>junit-jupiter</artifactId>
+      <scope>test</scope>
+    </dependency>
     <dependency>
       <groupId>org.apache.maven</groupId>
       <artifactId>maven-cli</artifactId>
diff --git a/apache-maven/src/assembly/maven/bin/JvmConfigParser.java 
b/apache-maven/src/assembly/maven/bin/JvmConfigParser.java
index 93b8011568..478e06981d 100644
--- a/apache-maven/src/assembly/maven/bin/JvmConfigParser.java
+++ b/apache-maven/src/assembly/maven/bin/JvmConfigParser.java
@@ -77,11 +77,11 @@ public static void main(String[] args) {
                 System.out.print(result);
                 System.out.flush();
             }
-        } catch (IOException e) {
-            // If jvm.config exists but can't be read, this is a configuration 
error
+        } catch (IOException | IllegalArgumentException e) {
+            // If jvm.config exists but can't be read or parsed, this is a 
configuration error
             // Print clear error and exit with error code to prevent Maven 
from running
-            System.err.println("ERROR: Failed to read .mvn/jvm.config: " + 
e.getMessage());
-            System.err.println("Please check file permissions and syntax.");
+            System.err.println("ERROR: Failed to process .mvn/jvm.config: " + 
e.getMessage());
+            System.err.println("Please check that the file is readable and has 
valid syntax.");
             System.err.flush();
             System.exit(1);
         }
@@ -149,37 +149,47 @@ private static void appendQuotedArguments(StringBuilder 
result, List<String> arg
 
     /**
      * Parse a line into individual arguments, respecting quoted strings.
-     * Quotes are stripped from the arguments.
+     * Quotes are stripped from the arguments. Empty quoted strings ({@code 
""} or {@code ''})
+     * produce an empty argument token. Backslashes are treated as literal 
characters;
+     * they do not act as escape characters.
      */
     private static List<String> parseArguments(String line) {
         List<String> args = new ArrayList<>();
         StringBuilder current = new StringBuilder();
         boolean inDoubleQuotes = false;
         boolean inSingleQuotes = false;
+        boolean tokenStarted = false;
 
         for (int i = 0; i < line.length(); i++) {
             char c = line.charAt(i);
 
             if (c == '"' && !inSingleQuotes) {
                 inDoubleQuotes = !inDoubleQuotes;
+                tokenStarted = true;
             } else if (c == '\'' && !inDoubleQuotes) {
                 inSingleQuotes = !inSingleQuotes;
-            } else if (c == ' ' && !inDoubleQuotes && !inSingleQuotes) {
-                // Space outside quotes - end of argument
-                if (current.length() > 0) {
+                tokenStarted = true;
+            } else if (Character.isWhitespace(c) && !inDoubleQuotes && 
!inSingleQuotes) {
+                if (tokenStarted) {
                     args.add(current.toString());
                     current.setLength(0);
+                    tokenStarted = false;
                 }
             } else {
                 current.append(c);
+                tokenStarted = true;
             }
         }
 
-        // Add last argument
-        if (current.length() > 0) {
+        if (tokenStarted) {
             args.add(current.toString());
         }
 
+        if (inDoubleQuotes || inSingleQuotes) {
+            throw new IllegalArgumentException(
+                    "Unclosed " + (inDoubleQuotes ? "double" : "single") + " 
quote in: " + line);
+        }
+
         return args;
     }
-}
\ No newline at end of file
+}
diff --git a/apache-maven/src/test/java/JvmConfigParserTest.java 
b/apache-maven/src/test/java/JvmConfigParserTest.java
new file mode 100644
index 0000000000..4f570905a0
--- /dev/null
+++ b/apache-maven/src/test/java/JvmConfigParserTest.java
@@ -0,0 +1,90 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.stream.Stream;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.junit.jupiter.params.provider.ValueSource;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+class JvmConfigParserTest {
+    @TempDir
+    Path tempDir;
+
+    static Stream<Arguments> arguments() {
+        return Stream.of(
+                // Empty quoted strings produce empty argument tokens
+                Arguments.of("first \"\" third", "\"first\" \"\" \"third\""),
+                Arguments.of("'' \"\"", "\"\" \"\""),
+                // Tab-separated tokens (whitespace generalisation)
+                Arguments.of("first\t\"\"\tthird", "\"first\" \"\" \"third\""),
+                // Adjacent quoted fragments merge into one token
+                Arguments.of("fi\"\"rst ''third", "\"first\" \"third\""),
+                // Space inside double quotes is preserved
+                Arguments.of("-Dname=\"hello world\"", "\"-Dname=hello 
world\""),
+                // Windows paths inside double quotes: backslash is literal
+                Arguments.of(
+                        "\"-javaagent:D:\\a\\maven\\target\\agent.jar\"",
+                        
"\"-javaagent:D:\\\\a\\\\maven\\\\target\\\\agent.jar\""),
+                // Single-quoted Windows path: backslash is literal
+                Arguments.of("'-Dpath=C:\\temp\\config'", 
"\"-Dpath=C:\\\\temp\\\\config\""),
+                // Blank / whitespace-only line produces empty output
+                Arguments.of("  \t  ", ""));
+    }
+
+    @ParameterizedTest
+    @MethodSource("arguments")
+    void tokenizesArguments(String input, String expected) throws Exception {
+        Path config = tempDir.resolve("jvm.config");
+        Files.writeString(config, input);
+        assertEquals(expected, JvmConfigParser.parseJvmConfig(config, 
tempDir.toString()));
+    }
+
+    @ParameterizedTest
+    @ValueSource(strings = {"\"\" '", "'' \""})
+    void rejectsUnclosedQuotes(String input) throws Exception {
+        Path config = tempDir.resolve("jvm.config");
+        Files.writeString(config, input);
+        assertThrows(IllegalArgumentException.class, () -> 
JvmConfigParser.parseJvmConfig(config, tempDir.toString()));
+    }
+
+    @Test
+    void preservesProjectBasedirAfterTokenization() throws Exception {
+        Path config = tempDir.resolve("jvm.config");
+        Files.writeString(config, "'-Dfirst=${MAVEN_PROJECTBASEDIR}/first' 
\"-Dsecond=$MAVEN_PROJECTBASEDIR/second\"");
+        assertEquals(
+                "\"-Dfirst=/tmp/maven/first\" \"-Dsecond=/tmp/maven/second\"",
+                JvmConfigParser.parseJvmConfig(config, "/tmp/maven"));
+    }
+
+    @Test
+    void preservesWindowsProjectBasedirBackslashes() throws Exception {
+        Path config = tempDir.resolve("jvm.config");
+        Files.writeString(config, "-Dfirst=${MAVEN_PROJECTBASEDIR}/first");
+        // Windows backslashes in the substituted path must be preserved as 
literal characters
+        assertEquals("\"-Dfirst=D:\\\\a\\\\maven/first\"", 
JvmConfigParser.parseJvmConfig(config, "D:\\a\\maven"));
+    }
+}

Reply via email to