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

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

commit 52347ac29b0152670ed954ffebb10ed06b7d5c10
Author: Guillaume Nodet <[email protected]>
AuthorDate: Fri Jun 26 09:55:21 2026 +0000

    Fix mvn.cmd jvm.config read failing silently on Windows CI
    
    Replace 'for /f' with 'set /p' + input redirect to read the
    JvmConfigParser temp file. 'for /f' silently produces no output
    when the file is briefly locked by Windows Defender real-time
    scanning, leaving JVM_CONFIG_MAVEN_OPTS empty and causing
    MavenITmng4559 tests to fail intermittently on Windows CI runners.
    
    'set /p <file' uses a transient input-redirect open with different
    sharing flags, making it far less susceptible to lock contention.
    If the file IS locked, it emits a visible error to stderr rather
    than failing silently. A single retry with a brief delay is added
    as a safety net.
    
    Direct stdout capture ('for /f ... in (`command`)') was tried
    previously (343b518a) and reverted (7685323d) because cmd.exe's
    child shell interprets special characters (pipes, ampersands) in
    the captured output, breaking jvm.config values like
    -Dhttp.nonProxyHosts=de|*.de.
    
    Co-Authored-By: Claude Opus 4.6 <[email protected]>
---
 apache-maven/src/assembly/maven/bin/mvn.cmd | 29 +++++++++++++++++++++++++----
 1 file changed, 25 insertions(+), 4 deletions(-)

diff --git a/apache-maven/src/assembly/maven/bin/mvn.cmd 
b/apache-maven/src/assembly/maven/bin/mvn.cmd
index f25f85858f..f6a1413077 100644
--- a/apache-maven/src/assembly/maven/bin/mvn.cmd
+++ b/apache-maven/src/assembly/maven/bin/mvn.cmd
@@ -184,7 +184,20 @@ if not exist "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto 
endReadJvmConfig
 
 rem Use Java source-launch mode (JDK 11+) to parse jvm.config
 rem This avoids batch script parsing issues with special characters (pipes, 
quotes, @, etc.)
-rem Use temp file approach with cmd /c to ensure proper file handle release
+rem Java writes parsed output to a temp file; we read it with 'set /p' + input 
redirect.
+rem
+rem Why 'set /p <file' instead of 'for /f ... in (file)':
+rem   - 'for /f' silently produces no output when the file is briefly locked
+rem     (e.g. by Windows Defender real-time scanning), leaving 
JVM_CONFIG_MAVEN_OPTS empty
+rem     and causing hard-to-diagnose Maven startup failures.
+rem   - 'set /p <file' uses a transient input-redirect open with different 
sharing flags,
+rem     making it far less susceptible to lock contention. If the file IS 
locked, it
+rem     emits a visible error to stderr rather than failing silently.
+rem
+rem Why not capture stdout directly ('for /f ... in (`command`)')?
+rem   - cmd.exe's child shell interprets special characters (pipes |, 
ampersands &, etc.)
+rem     in the captured output, which breaks jvm.config values like
+rem     -Dhttp.nonProxyHosts=de|*.de  (the core use case for JvmConfigParser).
 
 set "JVM_CONFIG_TEMP=%TEMP%\mvn-jvm-config-%RANDOM%-%RANDOM%.txt"
 
@@ -196,7 +209,7 @@ if defined MAVEN_DEBUG_SCRIPT (
   echo [DEBUG] Parser arguments: "%MAVEN_HOME%\bin\JvmConfigParser.java" 
"%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" "%MAVEN_PROJECTBASEDIR%" 
"%JVM_CONFIG_TEMP%"
 )
 
-rem Run parser with output file as third argument - Java writes directly to 
file to avoid Windows file locking issues
+rem Run parser with output file as third argument - Java writes directly to 
file
 "%JAVACMD%" "%MAVEN_HOME%\bin\JvmConfigParser.java" 
"%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" "%MAVEN_PROJECTBASEDIR%" 
"%JVM_CONFIG_TEMP%"
 set JVM_CONFIG_EXIT=%ERRORLEVEL%
 
@@ -215,13 +228,21 @@ if %JVM_CONFIG_EXIT% neq 0 (
   exit /b 1
 )
 
-rem Read the output file
+rem Read the output file using 'set /p' with input redirect (see comment above)
 if exist "%JVM_CONFIG_TEMP%" (
   if defined MAVEN_DEBUG_SCRIPT (
     echo [DEBUG] Temp file contents:
     type "%JVM_CONFIG_TEMP%"
   )
-  for /f "usebackq tokens=*" %%i in ("%JVM_CONFIG_TEMP%") do set 
"JVM_CONFIG_MAVEN_OPTS=%%i"
+  set /p JVM_CONFIG_MAVEN_OPTS=<"%JVM_CONFIG_TEMP%" 2>nul
+  rem Retry once after a brief delay if the read failed (Windows Defender file 
lock)
+  if not defined JVM_CONFIG_MAVEN_OPTS (
+    if defined MAVEN_DEBUG_SCRIPT (
+      echo [DEBUG] First read returned empty, retrying after delay...
+    )
+    ping -n 2 127.0.0.1 >nul 2>nul
+    set /p JVM_CONFIG_MAVEN_OPTS=<"%JVM_CONFIG_TEMP%" 2>nul
+  )
   del "%JVM_CONFIG_TEMP%" 2>nul
 )
 

Reply via email to