Skip to content

Commit 2bc6f84

Browse files
[MWRAPPER-161] Improve script directory detection when using sh mvnw (#329)
* [MWRAPPER-161] Improve script directory detection when using sh mvnw This PR fixes an issue where the Maven wrapper script (mvnw) incorrectly detects its directory when invoked via sh mvnw. The updated logic ensures reliable detection of the script’s location in all supported usage scenarios. Fixes: #305 * fix: add integration test Add an integration test that runs `sh mvnw`. * fix: disable integration test on Windows The integration test relies on `/bin/sh`, which is not available by default on Windows systems. While some environments may provide a compatible shell, this cannot be assumed across all Windows setups, making the test unreliable on that platform. Co-authored-by: Slawomir Jaranowski <s.jaranowski@gmail.com> * fix: wrong command in `mvnwDebug` script Co-authored-by: Slawomir Jaranowski <s.jaranowski@gmail.com> --------- Co-authored-by: Slawomir Jaranowski <s.jaranowski@gmail.com>
1 parent 4c4b8bf commit 2bc6f84

File tree

6 files changed

+140
-4
lines changed

6 files changed

+140
-4
lines changed

maven-wrapper-distribution/src/resources/only-mvnw

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,17 @@ trim() {
105105
printf "%s" "${1}" | tr -d '[:space:]'
106106
}
107107

108+
scriptDir="$(dirname "$0")"
109+
scriptName="$(basename "$0")"
110+
108111
# parse distributionUrl and optional distributionSha256Sum, requires .mvn/wrapper/maven-wrapper.properties
109112
while IFS="=" read -r key value; do
110113
case "${key-}" in
111114
distributionUrl) distributionUrl=$(trim "${value-}") ;;
112115
distributionSha256Sum) distributionSha256Sum=$(trim "${value-}") ;;
113116
esac
114-
done <"${0%/*}/.mvn/wrapper/maven-wrapper.properties"
115-
[ -n "${distributionUrl-}" ] || die "cannot read distributionUrl property in ${0%/*}/.mvn/wrapper/maven-wrapper.properties"
117+
done <"$scriptDir/.mvn/wrapper/maven-wrapper.properties"
118+
[ -n "${distributionUrl-}" ] || die "cannot read distributionUrl property in $scriptDir/.mvn/wrapper/maven-wrapper.properties"
116119

117120
case "${distributionUrl##*/}" in
118121
maven-mvnd-*bin.*)
@@ -130,7 +133,7 @@ maven-mvnd-*bin.*)
130133
distributionUrl="${distributionUrl%-bin.*}-$distributionPlatform.zip"
131134
;;
132135
maven-mvnd-*) MVN_CMD=mvnd.sh _MVNW_REPO_PATTERN=/maven/mvnd/ ;;
133-
*) MVN_CMD="mvn${0##*/mvnw}" _MVNW_REPO_PATTERN=/org/apache/maven/ ;;
136+
*) MVN_CMD="mvn${scriptName#mvnw}" _MVNW_REPO_PATTERN=/org/apache/maven/ ;;
134137
esac
135138

136139
# apply MVNW_REPOURL and calculate MAVEN_HOME

maven-wrapper-distribution/src/resources/only-mvnwDebug

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@
2121
# Apache Maven Wrapper debug script, version @@project.version@@
2222
# -----------------------------------------------------------------------------
2323

24-
. "${0%/*}"/mvnw
24+
. "$(dirname "$0")"/mvnw
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
# `/bin/sh` is not available by default on Windows systems
19+
invoker.os.family = !windows
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
3+
<!--
4+
Licensed to the Apache Software Foundation (ASF) under one
5+
or more contributor license agreements. See the NOTICE file
6+
distributed with this work for additional information
7+
regarding copyright ownership. The ASF licenses this file
8+
to you under the Apache License, Version 2.0 (the
9+
"License"); you may not use this file except in compliance
10+
with the License. You may obtain a copy of the License at
11+
12+
http://www.apache.org/licenses/LICENSE-2.0
13+
14+
Unless required by applicable law or agreed to in writing,
15+
software distributed under the License is distributed on an
16+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
KIND, either express or implied. See the License for the
18+
specific language governing permissions and limitations
19+
under the License.
20+
-->
21+
22+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
23+
<modelVersion>4.0.0</modelVersion>
24+
25+
<groupId>org.apache.maven.plugins.it.wrapper</groupId>
26+
<artifactId>extension</artifactId>
27+
<version>1.0.0-SNAPSHOT</version>
28+
<packaging>pom</packaging>
29+
30+
<build>
31+
<pluginManagement>
32+
<plugins>
33+
<plugin>
34+
<groupId>org.codehaus.mojo</groupId>
35+
<artifactId>exec-maven-plugin</artifactId>
36+
<version>3.0.0</version>
37+
<configuration>
38+
<executable>/bin/sh</executable>
39+
<arguments>
40+
<argument>mvnw</argument>
41+
<argument>-v</argument>
42+
</arguments>
43+
<environmentVariables>
44+
<MVNW_VERBOSE>true</MVNW_VERBOSE>
45+
<HOME>${project.build.directory}</HOME>
46+
<USERPROFILE>${project.build.directory}</USERPROFILE>
47+
</environmentVariables>
48+
</configuration>
49+
</plugin>
50+
</plugins>
51+
</pluginManagement>
52+
</build>
53+
</project>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
type=only-script
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
/*
3+
* Licensed to the Apache Software Foundation (ASF) under one
4+
* or more contributor license agreements. See the NOTICE file
5+
* distributed with this work for additional information
6+
* regarding copyright ownership. The ASF licenses this file
7+
* to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance
9+
* with the License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing,
14+
* software distributed under the License is distributed on an
15+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
* KIND, either express or implied. See the License for the
17+
* specific language governing permissions and limitations
18+
* under the License.
19+
*/
20+
21+
assert new File(basedir,'mvnw').exists()
22+
assert new File(basedir,'mvnw.cmd').exists()
23+
assert !(new File(basedir,'mvnwDebug').exists())
24+
assert !(new File(basedir,'mvnwDebug.cmd').exists())
25+
26+
wrapperProperties = new File(basedir,'.mvn/wrapper/maven-wrapper.properties')
27+
assert wrapperProperties.exists()
28+
assert !wrapperProperties.text.contains('wrapperUrl')
29+
30+
log = new File(basedir, 'build.log').text
31+
// check "mvn wrapper:wrapper" output
32+
assert log.contains('[INFO] Unpacked only-script type wrapper distribution org.apache.maven.wrapper:maven-wrapper-distribution:zip:only-script:')
33+
34+
// check "mvnw -v" output
35+
assert log.contains("Couldn't find ")
36+
assert log.contains(", downloading and installing it ...")
37+
38+
Properties props = new Properties()
39+
new File(basedir,'.mvn/wrapper/maven-wrapper.properties').withInputStream {
40+
props.load(it)
41+
}
42+
assert props.wrapperVersion.equals(wrapperCurrentVersion)
43+
assert props.distributionType.equals("only-script")

0 commit comments

Comments
 (0)