apache / apache/maven-wrapper

mvnw.cmd selects the last matching extracted directory while mvnw selects the first

Open Beginner friendly
#442 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
254
Forks
78
Avg merge
5h 26m
Merged PRs (30d)
2

Description

## Description

When the extracted archive directory name does not match the archive filename, both wrapper scripts fall back to scanning the extraction directory for a subdirectory containing `bin/$MVN_CMD`. The two implementations disagree on which candidate wins.

`mvnw` stops at the **first** match:

```sh
set +f
for dir in "$TMP_DOWNLOAD_DIR"/*; do
if [ -d "$dir" ]; then
if [ -f "$dir/bin/$MVN_CMD" ]; then
actualDistributionDir="$(basename "$dir")"
break
fi
fi
done
set -f
```

`mvnw.cmd` has no early exit, so the **last** match in enumeration order wins:

```powershell
if (!$actualDistributionDir) {
Get-ChildItem -Path "$TMP_DOWNLOAD_DIR" -Directory | ForEach-Object {
$testPath = Join-Path $_.FullName "bin/$MVN_CMD"
if (Test-Path -Path $testPath -PathType Leaf) {
$actualDistributionDir = $_.Name
}
}
}
```

With more than one candidate directory, the two scripts can therefore select different distributions on the same archive, and the Windows result depends on `Get-ChildItem` enumeration order.

## Suggested fix

Give `mvnw.cmd` first-match semantics to match `mvnw` — for example a `foreach` with `break`, or `Select-Object -First 1`:

```powershell
$actualDistributionDir = (Get-ChildItem -Path "$TMP_DOWNLOAD_DIR" -Directory |
Where-Object { Test-Path -Path (Join-Path $_.FullName "bin/$MVN_CMD") -PathType Leaf } |
Select-Object -First 1).Name
```

Erroring when more than one candidate is found would also be reasonable, provided both scripts agree.

## Environment

maven-wrapper 3.3.4, `distributionType=only-script`, Maven 3.9.16.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start in mvnw.cmd at the fallback Get-ChildItem scan shown in the issue, then compare its candidate selection with the corresponding loop in mvnw. Reproduce the case with multiple extracted directories containing bin/$MVN_CMD and verify that both wrappers select the first matching directory consistently.

Written by the indexing model from the issue text.

Assessment

Tech stack
powershell
Domain
build-system
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
76/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.