Skip to content

Commit 008d43e

Browse files
committed
[gh-11363] Fix pipe symbol handling in jvm.config on Windows
Use double nested for loops with eol=# and tokens=1 delims=# to handle comments, then use delayed expansion only for MAVEN_PROJECTBASEDIR substitution. The key insight from Maven 3 is that using %%a/%%b directly (parse-time expansion) works with pipes. We can safely store %%b in a variable for substitution because the parse-time expansion happens before the set command executes, storing pipes as literal characters. Solution: Double nested for loops + safe delayed expansion - Outer loop: eol=# skips full-line comments, tokens=1 delims=# extracts part before # - Inner loop: tokens=* trims spaces and skips empty lines - Store %%b in variable for MAVEN_PROJECTBASEDIR substitution (safe because %%b is parse-time) - Both loops use parse-time expansion (%%a, %%b), avoiding pipe interpretation This approach: - Handles full-line comments (lines starting with #) via eol=# - Handles end-of-line comments (everything after #) via delims=# - Trims leading/trailing whitespace via tokens=* - Replaces ${MAVEN_PROJECTBASEDIR} and $MAVEN_PROJECTBASEDIR placeholders - Skips empty lines automatically - Handles pipe symbols correctly (not interpreted as command separators) - Is simple and maintainable (21 lines)
1 parent 59628bf commit 008d43e

File tree

1 file changed

+13
-55
lines changed
  • apache-maven/src/assembly/maven/bin

1 file changed

+13
-55
lines changed

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

Lines changed: 13 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -181,63 +181,21 @@ if not exist "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadJvmConfig
181181

182182
@setlocal EnableExtensions EnableDelayedExpansion
183183
set "JVM_CONFIG_MAVEN_OPTS="
184-
rem Read jvm.config line by line and concatenate with spaces
185-
rem Users must properly quote values containing special characters (pipes, etc.)
186-
for /F "usebackq tokens=* delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do (
187-
set "line=%%a"
188-
189-
rem Simple comment removal - everything after first # is removed
190-
rem This matches the Unix sed behavior: sed -e 's/#.*$//'
191-
set "processed=!line:#= !"
192-
for /f "tokens=1 delims=#" %%b in ("!line!") do set "processed=%%b"
193-
194-
rem Trim leading spaces
195-
:trimLeading%%a
196-
if "!processed:~0,1!"==" " (
197-
set "processed=!processed:~1!"
198-
goto trimLeading%%a
199-
)
200-
201-
rem Trim trailing spaces
202-
:trimTrailing%%a
203-
if "!processed:~-1!"==" " (
204-
set "processed=!processed:~0,-1!"
205-
goto trimTrailing%%a
206-
)
207-
208-
rem Skip empty lines
209-
if not "!processed!"=="" (
210-
rem Replace MAVEN_PROJECTBASEDIR placeholders
211-
set "processed=!processed:${MAVEN_PROJECTBASEDIR}=%MAVEN_PROJECTBASEDIR%!"
212-
set "processed=!processed:$MAVEN_PROJECTBASEDIR=%MAVEN_PROJECTBASEDIR%!"
213-
214-
rem Quote unquoted pipe symbols in -D property values
215-
rem Check if line contains unquoted pipes
216-
set "testPipe=!processed:|=!"
217-
if not "!testPipe!"=="!processed!" (
218-
rem Has pipes - check if it's a -D property
219-
if "!processed:~0,2!"=="-D" (
220-
rem Find the = sign and check if value is already quoted
221-
for /f "tokens=1,* delims==" %%p in ("!processed!") do (
222-
set "propName=%%p"
223-
set "propValue=%%q"
224-
if defined propValue (
225-
set "firstChar=!propValue:~0,1!"
226-
set "lastChar=!propValue:~-1!"
227-
if not "!firstChar!"=="^"" if not "!lastChar!"=="^"" (
228-
rem Value is not quoted, quote it
229-
set "processed=!propName!="!propValue!""
230-
)
231-
)
232-
)
233-
)
234-
)
235-
236-
rem Concatenate with space separator
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%!"
237195
if "!JVM_CONFIG_MAVEN_OPTS!"=="" (
238-
set "JVM_CONFIG_MAVEN_OPTS=!processed!"
196+
set "JVM_CONFIG_MAVEN_OPTS=!line!"
239197
) else (
240-
set "JVM_CONFIG_MAVEN_OPTS=!JVM_CONFIG_MAVEN_OPTS! !processed!"
198+
set "JVM_CONFIG_MAVEN_OPTS=!JVM_CONFIG_MAVEN_OPTS! !line!"
241199
)
242200
)
243201
)

0 commit comments

Comments
 (0)