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

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


The following commit(s) were added to refs/heads/maven-4.0.x by this push:
     new 66dfc7d879 Give each mvn.cmd run its own temp directory (#13716)
66dfc7d879 is described below

commit 66dfc7d8794520d43733dadb5c698997ad75bc60
Author: Sylwester Lachiewicz <[email protected]>
AuthorDate: Mon Oct 26 11:30:29 2026 +0100

    Give each mvn.cmd run its own temp directory (#13716)
    
    %RANDOM% is seeded from the clock once per cmd.exe, so mvn.cmd processes
    started in the same second named their jvm.config temp file alike. Since the
    JVM reads it as an @argfile and it is kept until the JVM exits, parallel 
builds
    ran with each other's JVM arguments. mkdir fails on an existing name, so a
    retry loop yields a private directory.
---
 apache-maven/src/assembly/maven/bin/mvn.cmd | 34 ++++++++++++++++++++++-------
 1 file changed, 26 insertions(+), 8 deletions(-)

diff --git a/apache-maven/src/assembly/maven/bin/mvn.cmd 
b/apache-maven/src/assembly/maven/bin/mvn.cmd
index f370e2ad8f..3001b3e988 100644
--- a/apache-maven/src/assembly/maven/bin/mvn.cmd
+++ b/apache-maven/src/assembly/maven/bin/mvn.cmd
@@ -177,8 +177,10 @@ cd /d "%EXEC_DIR%"
 
 :endDetectBaseDir
 
-rem Initialize JVM_CONFIG_ARGFILE_ARG to empty
+rem Initialize to empty so values inherited from the environment are never 
used or deleted
 set "JVM_CONFIG_ARGFILE_ARG="
+set "JVM_CONFIG_DIR="
+set "JVM_CONFIG_TEMP="
 
 if not exist "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadJvmConfig
 
@@ -191,7 +193,12 @@ rem Why write to a temp file instead of capturing stdout 
via 'for /f ... in (`cm
 rem   cmd.exe's child shell interprets special characters (pipes |, ampersands 
&, etc.)
 rem   in the captured output — breaking values like 
-Dhttp.nonProxyHosts=de|*.de.
 
-set "JVM_CONFIG_TEMP=%TEMP%\mvn-jvm-config-%RANDOM%-%RANDOM%.txt"
+call :mkTempDir jvm-config JVM_CONFIG_DIR
+if ERRORLEVEL 1 (
+  echo ERROR: Could not create a temporary directory in %TEMP% for 
.mvn/jvm.config 1>&2
+  exit /b 1
+)
+set "JVM_CONFIG_TEMP=%JVM_CONFIG_DIR%\jvm.config.args"
 
 rem Debug logging (set MAVEN_DEBUG_SCRIPT=1 to enable)
 if defined MAVEN_DEBUG_SCRIPT (
@@ -214,9 +221,7 @@ if %JVM_CONFIG_EXIT% neq 0 (
   echo ERROR: Failed to parse .mvn/jvm.config file 1>&2
   echo   jvm.config path: %MAVEN_PROJECTBASEDIR%\.mvn\jvm.config 1>&2
   echo   Java command: %JAVACMD% 1>&2
-  if exist "%JVM_CONFIG_TEMP%" (
-    del "%JVM_CONFIG_TEMP%" 2>nul
-  )
+  rd /s /q "%JVM_CONFIG_DIR%" 2>nul
   exit /b 1
 )
 
@@ -307,8 +312,8 @@ if defined MAVEN_DEBUG_SCRIPT (
   %MAVEN_ARGS% ^
   %*
 set JVM_CONFIG_EXIT_CODE=%ERRORLEVEL%
-rem Cleanup the jvm.config temp file now that the JVM has exited (must be 
before @endlocal)
-if defined JVM_CONFIG_TEMP del "%JVM_CONFIG_TEMP%" 2>nul
+rem Cleanup the jvm.config temp directory now that the JVM has exited (must be 
before @endlocal)
+if defined JVM_CONFIG_DIR rd /s /q "%JVM_CONFIG_DIR%" 2>nul
 if %JVM_CONFIG_EXIT_CODE% neq 0 goto error
 goto end
 
@@ -329,4 +334,17 @@ if exist "%USERPROFILE%\mavenrc_post.cmd" call 
"%USERPROFILE%\mavenrc_post.cmd"
 @REM pause the script if MAVEN_BATCH_PAUSE is set to 'on'
 if "%MAVEN_BATCH_PAUSE%"=="on" pause
 
-exit /b %ERROR_CODE%
\ No newline at end of file
+exit /b %ERROR_CODE%
+
+:mkTempDir
+@REM Creates a new, empty directory under %TEMP% and stores its path in the 
variable named by %2.
+@REM %RANDOM% is seeded from the clock once per cmd.exe, so mvn.cmd processes 
started in the same
+@REM second draw the same numbers. mkdir fails when the name is taken and the 
retry moves on to the
+@REM next number, so concurrent builds never share a temp file.
+set "_MKTEMP_TRIES=0"
+:mkTempDirRetry
+set /a _MKTEMP_TRIES+=1
+set "_MKTEMP_DIR=%TEMP%\mvn-%~1-%RANDOM%-%RANDOM%"
+mkdir "%_MKTEMP_DIR%" 2>nul && (set "%~2=%_MKTEMP_DIR%" & exit /b 0)
+if %_MKTEMP_TRIES% lss 100 goto mkTempDirRetry
+exit /b 1

Reply via email to