Skip to content

Commit c2acb11

Browse files
committed
[gh-11363] Fix pipe symbol handling in jvm.config on Windows using Java parser
Windows batch scripts have fundamental issues parsing special characters like pipes, even with various escaping techniques. Instead of complex batch script parsing, use a simple Java parser that can be executed directly. Changes: - Created JvmConfigParser.java: Simple Java file that parses jvm.config - Windows (mvn.cmd): Call Java parser instead of batch script parsing (3 lines) - Unix (mvn): Keep existing awk-based parsing (works fine) - Tests: Keep multiple arguments per line (works on Unix, now works on Windows) Benefits: - Handles all special characters correctly (pipes, quotes, etc.) - Much simpler Windows code (3 lines vs 17+ lines of complex batch script) - Consistent behavior across platforms - Uses Java 11+ single-file source-code execution (no compilation needed) - Handles comments, MAVEN_PROJECTBASEDIR substitution, etc.
1 parent 008d43e commit c2acb11

File tree

3 files changed

+89
-23
lines changed

3 files changed

+89
-23
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
import java.io.IOException;
21+
import java.nio.charset.StandardCharsets;
22+
import java.nio.file.Files;
23+
import java.nio.file.Path;
24+
import java.nio.file.Paths;
25+
import java.util.stream.Stream;
26+
27+
/**
28+
* Parses .mvn/jvm.config file for Windows batch scripts.
29+
* This avoids the complexity of parsing special characters (pipes, quotes, etc.) in batch scripts.
30+
*
31+
* Usage: java JvmConfigParser.java <jvm.config-path> <maven-project-basedir>
32+
*
33+
* Outputs: Space-separated JVM arguments with proper escaping for batch scripts
34+
*/
35+
public class JvmConfigParser {
36+
public static void main(String[] args) {
37+
if (args.length != 2) {
38+
System.err.println("Usage: java JvmConfigParser.java <jvm.config-path> <maven-project-basedir>");
39+
System.exit(1);
40+
}
41+
42+
Path jvmConfigPath = Paths.get(args[0]);
43+
String mavenProjectBasedir = args[1];
44+
45+
if (!Files.exists(jvmConfigPath)) {
46+
// No jvm.config file - output nothing
47+
return;
48+
}
49+
50+
try (Stream<String> lines = Files.lines(jvmConfigPath, StandardCharsets.UTF_8)) {
51+
StringBuilder result = new StringBuilder();
52+
53+
lines.forEach(line -> {
54+
// Remove comments
55+
int commentIndex = line.indexOf('#');
56+
if (commentIndex >= 0) {
57+
line = line.substring(0, commentIndex);
58+
}
59+
60+
// Trim whitespace
61+
line = line.trim();
62+
63+
// Skip empty lines
64+
if (line.isEmpty()) {
65+
return;
66+
}
67+
68+
// Replace MAVEN_PROJECTBASEDIR placeholders
69+
line = line.replace("${MAVEN_PROJECTBASEDIR}", mavenProjectBasedir);
70+
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);
77+
});
78+
79+
System.out.print(result.toString());
80+
} catch (IOException e) {
81+
System.err.println("Error reading jvm.config: " + e.getMessage());
82+
System.exit(1);
83+
}
84+
}
85+
}
86+

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,6 @@ handle_args() {
256256
done
257257
}
258258

259-
260-
261259
handle_args "$@"
262260
MAVEN_MAIN_CLASS=${MAVEN_MAIN_CLASS:=org.apache.maven.cling.MavenCling}
263261

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

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -179,27 +179,9 @@ cd /d "%EXEC_DIR%"
179179

180180
if not exist "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadJvmConfig
181181

182-
@setlocal EnableExtensions EnableDelayedExpansion
183-
set "JVM_CONFIG_MAVEN_OPTS="
184-
rem Read jvm.config line by line, using eol=# and tokens=1 delims=# to strip comments
185-
rem Then trim whitespace - both %%a and %%b use parse-time expansion to avoid pipe interpretation
186-
for /F "usebackq eol=# tokens=1 delims=#" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do (
187-
rem %%a contains everything before # (comments removed by delims=#, full-line comments skipped by eol=#)
188-
rem Trim leading/trailing spaces and skip empty lines
189-
for /f "tokens=*" %%b in ("%%a") do (
190-
rem %%b has spaces trimmed, empty lines automatically skipped
191-
rem Now we need to do MAVEN_PROJECTBASEDIR substitution
192-
set "line=%%b"
193-
set "line=!line:${MAVEN_PROJECTBASEDIR}=%MAVEN_PROJECTBASEDIR%!"
194-
set "line=!line:$MAVEN_PROJECTBASEDIR=%MAVEN_PROJECTBASEDIR%!"
195-
if "!JVM_CONFIG_MAVEN_OPTS!"=="" (
196-
set "JVM_CONFIG_MAVEN_OPTS=!line!"
197-
) else (
198-
set "JVM_CONFIG_MAVEN_OPTS=!JVM_CONFIG_MAVEN_OPTS! !line!"
199-
)
200-
)
201-
)
202-
@endlocal & set "JVM_CONFIG_MAVEN_OPTS=%JVM_CONFIG_MAVEN_OPTS%"
182+
rem Use Java to parse jvm.config to avoid batch script parsing issues with special characters
183+
rem This handles pipes, quotes, and other special characters correctly
184+
for /f "delims=" %%i in ('"%JAVACMD%" "%MAVEN_HOME%\bin\JvmConfigParser.java" "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" "%MAVEN_PROJECTBASEDIR%" 2^>nul') do set JVM_CONFIG_MAVEN_OPTS=%%i
203185

204186
:endReadJvmConfig
205187

0 commit comments

Comments
 (0)