fix(wrapper): resolve script directory from mvnw.cmd location instead of CWD#419
Open
armorbreak001 wants to merge 1 commit intoapache:masterfrom
Open
fix(wrapper): resolve script directory from mvnw.cmd location instead of CWD#419armorbreak001 wants to merge 1 commit intoapache:masterfrom
armorbreak001 wants to merge 1 commit intoapache:masterfrom
Conversation
… of CWD When mvnw.cmd is invoked from a parent directory (e.g., c:\project\module\mvnw.cmd from c:\project), EXEC_DIR was set to the current working directory instead of the script's own directory. This caused the wrapper to look for .mvn/wrapper/ in the wrong place. Use pushd/popd with %~dp0 to resolve EXEC_DIR from the script's location, matching the behavior of the Unix mvnw script which uses dirname "$0". Fixes apache#247
There was a problem hiding this comment.
Pull request overview
Fixes Maven Wrapper Windows script base-directory detection so mvnw.cmd works when invoked from a different working directory (e.g., a parent directory), aligning behavior more closely with the Unix mvnw script.
Changes:
- Temporarily
pushdinto%~dp0to computeEXEC_DIRfrom the script’s location rather than%CD%. popdback to the caller’s working directory after capturingEXEC_DIR.
Comments suppressed due to low confidence (1)
maven-wrapper-distribution/src/resources/mvnw.cmd:97
popdhappens before the base-dir search loop, but the loop relies oncd ../%CD%to walk up from the starting directory. With this change,WDIRstarts at the script dir, but the current directory is restored to the caller’s CWD before:findBaseDir, socd ..walks up from the wrong place whenever.mvnis not directly under the script directory (e.g.,.mvnis in an ancestor of%~dp0). Consider keeping thepushdin effect until after base-dir detection, or explicitlycd /dto%EXEC_DIR%before entering:findBaseDirso the traversal is anchored to the script location.
pushd "%~dp0" >nul 2>&1
set EXEC_DIR=%CD%
popd >nul 2>&1
set WDIR=%EXEC_DIR%
:findBaseDir
IF EXIST "%WDIR%"\.mvn goto baseDirFound
cd ..
IF "%WDIR%"=="%CD%" goto baseDirNotFound
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #247 — mvnw.cmd fails when run from a parent directory.
Problem
When invoking
mvnw.cmdfrom a parent directory, the script setsEXEC_DIRusing%CD%(current working directory) instead of resolving from the script's own location (%~dp0). This causes it to search for.mvn/wrapper/maven-wrapper.propertiesin the wrong directory:The Unix
mvnwscript does not have this problem — it correctly uses$(dirname "$0")to find the base directory.Fix
Wrap the
EXEC_DIRassignment withpushd/popd %~dp0so that%CD%resolves to the script's directory rather than the caller's working directory:This matches the behavior of the Unix shell script and is the approach suggested in the issue report.