Skip to content

Commit 0aca2e4

Browse files
committed
[gh-11363] Java parser outputs single line with quoted arguments
Instead of outputting one argument per line (which still causes pipe interpretation issues in batch for loops), output a single line with each argument already quoted. This is much simpler and avoids all batch script parsing issues. Quotes in arguments are escaped by doubling them (batch convention).
1 parent b17e958 commit 0aca2e4

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

apache-maven/src/assembly/maven/bin/JvmConfigParser.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
*
3131
* Usage: java JvmConfigParser.java <jvm.config-path> <maven-project-basedir>
3232
*
33-
* Outputs: One argument per line (for safe batch script processing)
33+
* Outputs: Single line with space-separated quoted arguments (safe for batch scripts)
3434
*/
3535
public class JvmConfigParser {
3636
public static void main(String[] args) {
@@ -48,6 +48,8 @@ public static void main(String[] args) {
4848
}
4949

5050
try (Stream<String> lines = Files.lines(jvmConfigPath, StandardCharsets.UTF_8)) {
51+
StringBuilder result = new StringBuilder();
52+
5153
lines.forEach(line -> {
5254
// Remove comments
5355
int commentIndex = line.indexOf('#');
@@ -67,9 +69,18 @@ public static void main(String[] args) {
6769
line = line.replace("${MAVEN_PROJECTBASEDIR}", mavenProjectBasedir);
6870
line = line.replace("$MAVEN_PROJECTBASEDIR", mavenProjectBasedir);
6971

70-
// Output each line separately (one argument per line)
71-
System.out.println(line);
72+
// Quote the argument to protect special characters
73+
// Escape any existing quotes by doubling them
74+
line = line.replace("\"", "\"\"");
75+
76+
// Append quoted argument with space separator
77+
if (result.length() > 0) {
78+
result.append(' ');
79+
}
80+
result.append('"').append(line).append('"');
7281
});
82+
83+
System.out.print(result.toString());
7384
} catch (IOException e) {
7485
System.err.println("Error reading jvm.config: " + e.getMessage());
7586
System.exit(1);

apache-maven/src/assembly/maven/bin/mvn.cmd

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -185,16 +185,11 @@ rem Compile the parser if not already compiled
185185
if not exist "%MAVEN_HOME%\bin\JvmConfigParser.class" (
186186
"%JAVACMD:java.exe=javac.exe%" -d "%MAVEN_HOME%\bin" "%MAVEN_HOME%\bin\JvmConfigParser.java" >nul 2>&1
187187
)
188-
rem Run the parser and save output to temp file (one argument per line)
188+
rem Run the parser and save output to temp file (single line with quoted arguments)
189189
set "JVM_CONFIG_TEMP=%TEMP%\mvn-jvm-config-%RANDOM%.txt"
190190
"%JAVACMD%" -cp "%MAVEN_HOME%\bin" JvmConfigParser "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" "%MAVEN_PROJECTBASEDIR%" > "%JVM_CONFIG_TEMP%" 2>nul
191-
rem Read each line and append to JVM_CONFIG_MAVEN_OPTS with proper quoting
192-
@setlocal EnableDelayedExpansion
193-
set "JVM_CONFIG_MAVEN_OPTS="
194-
for /f "usebackq delims=" %%a in ("%JVM_CONFIG_TEMP%") do (
195-
set "JVM_CONFIG_MAVEN_OPTS=!JVM_CONFIG_MAVEN_OPTS! "%%a""
196-
)
197-
@endlocal & set "JVM_CONFIG_MAVEN_OPTS=%JVM_CONFIG_MAVEN_OPTS%"
191+
rem Read the single line from temp file
192+
set /p JVM_CONFIG_MAVEN_OPTS=<"%JVM_CONFIG_TEMP%"
198193
del "%JVM_CONFIG_TEMP%" 2>nul
199194

200195
:endReadJvmConfig

0 commit comments

Comments
 (0)