Skip to content

Fix Windows Junction Link support in mvnw.cmd#143

Merged
gnodet merged 2 commits intoapache:masterfrom
fp024:dev-2024-05-30
Jul 21, 2025
Merged

Fix Windows Junction Link support in mvnw.cmd#143
gnodet merged 2 commits intoapache:masterfrom
fp024:dev-2024-05-30

Conversation

@fp024
Copy link
Contributor

@fp024 fp024 commented May 31, 2024

hello.

I applied to join Apache JIRA, but I wasn't sure if I would be approved as a member, so I submitted a PR for issue reporting purposes.

Issue

Move-Item fails when the .m2 path is in the following state:

  • Junction link reference status of m2 directory on my system
    image-20240530210414913
    • G:\Maven\.m2 is the actual path.

The argument value of -Destnation in PowerShell's Move-Item command must be an actual path, but in my environment, it is a junction link and is bound to fail.

Workaround

So I modified the code to get $MAVEN_HOME_PARENT as follows.

# ...
$MAVEN_M2_PATH = "$HOME/.m2"
if ($env:MAVEN_USER_HOME) {
  $MAVEN_M2_PATH = "$env:MAVEN_USER_HOME"
}

if (-not (Test-Path -Path $MAVEN_M2_PATH)) {
    New-Item -Path $MAVEN_M2_PATH -ItemType Directory | Out-Null
}

$MAVEN_WRAPPER_DISTS = $null
if ((Get-Item $MAVEN_M2_PATH).Target -eq $null) {
  $MAVEN_WRAPPER_DISTS = "$MAVEN_M2_PATH/wrapper/dists"
} else {
  $MAVEN_WRAPPER_DISTS = (Get-Item $MAVEN_M2_PATH).Target[0] + "/wrapper/dists"
}

$MAVEN_HOME_PARENT = "$MAVEN_WRAPPER_DISTS/$distributionUrlNameMain"
# ...
  • The actual path could be obtained by obtaining Target[0] on the Item obtained with Get-Item -Path {Path}.

    • If the path passed as an argument is an actual path rather than a junction link path, it is $null.

This PR commit was posted for issue reporting purposes only, so it may require expert editing.

Please confirm. thank you have a good day. 👍


Following this checklist to help us incorporate your
contribution quickly and easily:

  • Make sure there is a [JIRA issue](https://issues.apache.org/jira/browse/MWRAPPER) filed
    for the change (usually before you start working on it). Trivial changes like typos do not
    require a JIRA issue. Your pull request should address just this issue, without
    pulling in other changes.
  • Each commit in the pull request should have a meaningful subject line and body.
  • Format the pull request title like [MWRAPPER-XXX] - Fixes bug in ApproximateQuantiles,
    where you replace MWRAPPER-XXX with the appropriate JIRA issue. Best practice
    is to use the JIRA issue title in the pull request title and in the first line of the
    commit message.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Run mvn clean verify to make sure basic checks pass. A more thorough check will
    be performed on your pull request automatically.
  • You have run the integration tests successfully (mvn -Prun-its clean verify).

If your pull request is about ~20 lines of code you don't need to sign an
[Individual Contributor License Agreement](https://www.apache.org/licenses/icla.pdf) if you are unsure
please ask on the developers list.

To make clear that you license your contribution under
the [Apache License Version 2.0, January 2004](http://www.apache.org/licenses/LICENSE-2.0)
you have to acknowledge this by using the following check-box.

@cstamas
Copy link
Member

cstamas commented May 31, 2024

@michael-o ping

@fp024 fp024 force-pushed the dev-2024-05-30 branch from eaddcc6 to cefb225 Compare May 31, 2024 23:22
@fp024
Copy link
Contributor Author

fp024 commented May 31, 2024

hello.

I received a GitHub Action build failure email and checked the code again.

Here's what I saw in PowerShell ISE after separating that part:

# Set MAVEN_USER_HOME manually
# $env:MAVEN_USER_HOME="D:/MAVEN2/.m2"
$env:MAVEN_USER_HOME=$null

$MAVEN_M2_PATH = "$HOME/.m2"
if ($env:MAVEN_USER_HOME) {
  $MAVEN_M2_PATH = "$env:MAVEN_USER_HOME"
}
Write-Output "MAVEN_M2_PATH: $MAVEN_M2_PATH"

if (-not (Test-Path -Path $MAVEN_M2_PATH)) {
    New-Item -Path $MAVEN_M2_PATH -ItemType Directory | Out-Null
}

$MAVEN_WRAPPER_DISTS = $null
if ((Get-Item $MAVEN_M2_PATH).Target[0] -eq $null) { # 💡Re-edited part
  Write-Output "is null: actual path"
  $MAVEN_WRAPPER_DISTS = "$MAVEN_M2_PATH/wrapper/dists"
  Write-Output "MAVEN_WRAPPER_DISTS: $MAVEN_WRAPPER_DISTS"
} else {
  $MAVEN_WRAPPER_DISTS = (Get-Item $MAVEN_M2_PATH).Target[0] + "/wrapper/dists"
  Write-Output "not null: junction path -> actual path"
  Write-Output "MAVEN_WRAPPER_DISTS: $MAVEN_WRAPPER_DISTS"
}

When MAVEN_USER_HOME is not set as an environment variable

  • MAVEN_M2_PATH="HOME/.m2"

image-20240601082911919

  • C:\Users\{UserName}/.m2 is the junction path
    • G:\Maven\m2/wrapper/dists is the actual path
    • The actual path to the picture in the first comment is incorrect: my actual path environment is G:\Maven\m2, not G:\Maven\.m2.

When MAVEN_USER_HOME is set as an environment variable

  • MAVEN_M2_PATH="D:/MAVEN2/.m2"
    • D:/MAVEN2/.m2 is the actual path

image-20240601083148668

thank you 👍

@gnodet gnodet merged commit e0c91a1 into apache:master Jul 21, 2025
21 checks passed
@gnodet gnodet changed the title Workaround: Windows Junction Link handling issue in mvnw.cmd Fix Windows Junction Link support in mvnw.cmd (#143) Jul 21, 2025
@github-actions github-actions bot added this to the 3.3.3 milestone Jul 21, 2025
@github-actions
Copy link

@gnodet Please assign appropriate label to PR according to the type of change.

@gnodet gnodet changed the title Fix Windows Junction Link support in mvnw.cmd (#143) Fix Windows Junction Link support in mvnw.cmd Jul 21, 2025
@gnodet gnodet added the bug Something isn't working label Jul 21, 2025
@github-actions
Copy link

@gnodet Please assign appropriate label to PR according to the type of change.

2 similar comments
@github-actions
Copy link

@gnodet Please assign appropriate label to PR according to the type of change.

@github-actions
Copy link

@gnodet Please assign appropriate label to PR according to the type of change.

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.

3 participants