Skip to content

Commit db1fe7e

Browse files
committed
[gh-11363] Output one argument per line and quote each individually
The Java parser now outputs one argument per line. The batch script reads each line and quotes it individually. This prevents pipes and other special characters from being interpreted as command separators.
1 parent bd4b67d commit db1fe7e

File tree

2 files changed

+17
-18
lines changed

2 files changed

+17
-18
lines changed

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

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@
2727
/**
2828
* Parses .mvn/jvm.config file for Windows batch scripts.
2929
* This avoids the complexity of parsing special characters (pipes, quotes, etc.) in batch scripts.
30-
*
30+
*
3131
* Usage: java JvmConfigParser.java <jvm.config-path> <maven-project-basedir>
32-
*
33-
* Outputs: Space-separated JVM arguments with proper escaping for batch scripts
32+
*
33+
* Outputs: One argument per line (for safe batch script processing)
3434
*/
3535
public class JvmConfigParser {
3636
public static void main(String[] args) {
@@ -48,35 +48,28 @@ 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-
5351
lines.forEach(line -> {
5452
// Remove comments
5553
int commentIndex = line.indexOf('#');
5654
if (commentIndex >= 0) {
5755
line = line.substring(0, commentIndex);
5856
}
59-
57+
6058
// Trim whitespace
6159
line = line.trim();
62-
60+
6361
// Skip empty lines
6462
if (line.isEmpty()) {
6563
return;
6664
}
67-
65+
6866
// Replace MAVEN_PROJECTBASEDIR placeholders
6967
line = line.replace("${MAVEN_PROJECTBASEDIR}", mavenProjectBasedir);
7068
line = line.replace("$MAVEN_PROJECTBASEDIR", mavenProjectBasedir);
71-
72-
// Append to result with space separator
73-
if (result.length() > 0) {
74-
result.append(' ');
75-
}
76-
result.append(line);
69+
70+
// Output each line separately (one argument per line)
71+
System.out.println(line);
7772
});
78-
79-
System.out.print(result.toString());
8073
} catch (IOException e) {
8174
System.err.println("Error reading jvm.config: " + e.getMessage());
8275
System.exit(1);

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,16 @@ 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 to avoid pipe interpretation issues
188+
rem Run the parser and save output to temp file (one argument per line)
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-
set /p JVM_CONFIG_MAVEN_OPTS=<%JVM_CONFIG_TEMP%
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%
192198
del %JVM_CONFIG_TEMP% 2>nul
193199

194200
:endReadJvmConfig

0 commit comments

Comments
 (0)