Skip to content

fix(wrapper): resolve script directory from mvnw.cmd location instead of CWD#419

Open
armorbreak001 wants to merge 1 commit intoapache:masterfrom
armorbreak001:fix/247-mvnw-cmd-parent-dir
Open

fix(wrapper): resolve script directory from mvnw.cmd location instead of CWD#419
armorbreak001 wants to merge 1 commit intoapache:masterfrom
armorbreak001:fix/247-mvnw-cmd-parent-dir

Conversation

@armorbreak001
Copy link
Copy Markdown

Summary

Fixes #247mvnw.cmd fails when run from a parent directory.

Problem

When invoking mvnw.cmd from a parent directory, the script sets EXEC_DIR using %CD% (current working directory) instead of resolving from the script's own location (%~dp0). This causes it to search for .mvn/wrapper/maven-wrapper.properties in the wrong directory:

The system cannot find the file C:\project\.mvn\wrapper\maven-wrapper.properties

The Unix mvnw script does not have this problem — it correctly uses $(dirname "$0") to find the base directory.

Fix

Wrap the EXEC_DIR assignment with pushd/popd %~dp0 so that %CD% resolves to the script's directory rather than the caller's working directory:

+pushd "%~dp0" >nul 2>&1
 set EXEC_DIR=%CD%
+popd >nul 2>&1

This matches the behavior of the Unix shell script and is the approach suggested in the issue report.

… 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
@slachiewicz slachiewicz requested a review from Copilot April 22, 2026 13:44
@slachiewicz slachiewicz added the bug Something isn't working label Apr 22, 2026
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 pushd into %~dp0 to compute EXEC_DIR from the script’s location rather than %CD%.
  • popd back to the caller’s working directory after capturing EXEC_DIR.
Comments suppressed due to low confidence (1)

maven-wrapper-distribution/src/resources/mvnw.cmd:97

  • popd happens before the base-dir search loop, but the loop relies on cd .. / %CD% to walk up from the starting directory. With this change, WDIR starts at the script dir, but the current directory is restored to the caller’s CWD before :findBaseDir, so cd .. walks up from the wrong place whenever .mvn is not directly under the script directory (e.g., .mvn is in an ancestor of %~dp0). Consider keeping the pushd in effect until after base-dir detection, or explicitly cd /d to %EXEC_DIR% before entering :findBaseDir so 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[MWRAPPER-82] mvnw.cmd fails when run from a parent directory.

3 participants