This is an automated email from the ASF dual-hosted git repository.
sjaranowski pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-wrapper.git
The following commit(s) were added to refs/heads/master by this push:
new 2bc6f84 [MWRAPPER-161] Improve script directory detection when using
sh mvnw (#329)
2bc6f84 is described below
commit 2bc6f84c41a90ec2baa6fdcf41d2129a5bb081ed
Author: Piotr P. Karwasz <[email protected]>
AuthorDate: Wed Jul 9 09:21:57 2025 +0200
[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 <[email protected]>
* fix: wrong command in `mvnwDebug` script
Co-authored-by: Slawomir Jaranowski <[email protected]>
---------
Co-authored-by: Slawomir Jaranowski <[email protected]>
---
maven-wrapper-distribution/src/resources/only-mvnw | 9 ++--
.../src/resources/only-mvnwDebug | 2 +-
.../sh_type_only-script/invoker.properties | 15 ++----
.../src/it/projects/sh_type_only-script/pom.xml | 53 ++++++++++++++++++++++
.../projects/sh_type_only-script/test.properties | 14 ++----
.../it/projects/sh_type_only-script/verify.groovy | 43 ++++++++++++++++++
6 files changed, 112 insertions(+), 24 deletions(-)
diff --git a/maven-wrapper-distribution/src/resources/only-mvnw
b/maven-wrapper-distribution/src/resources/only-mvnw
index 21c1f01..32b3f16 100755
--- a/maven-wrapper-distribution/src/resources/only-mvnw
+++ b/maven-wrapper-distribution/src/resources/only-mvnw
@@ -105,14 +105,17 @@ trim() {
printf "%s" "${1}" | tr -d '[:space:]'
}
+scriptDir="$(dirname "$0")"
+scriptName="$(basename "$0")"
+
# parse distributionUrl and optional distributionSha256Sum, requires
.mvn/wrapper/maven-wrapper.properties
while IFS="=" read -r key value; do
case "${key-}" in
distributionUrl) distributionUrl=$(trim "${value-}") ;;
distributionSha256Sum) distributionSha256Sum=$(trim "${value-}") ;;
esac
-done <"${0%/*}/.mvn/wrapper/maven-wrapper.properties"
-[ -n "${distributionUrl-}" ] || die "cannot read distributionUrl property in
${0%/*}/.mvn/wrapper/maven-wrapper.properties"
+done <"$scriptDir/.mvn/wrapper/maven-wrapper.properties"
+[ -n "${distributionUrl-}" ] || die "cannot read distributionUrl property in
$scriptDir/.mvn/wrapper/maven-wrapper.properties"
case "${distributionUrl##*/}" in
maven-mvnd-*bin.*)
@@ -130,7 +133,7 @@ maven-mvnd-*bin.*)
distributionUrl="${distributionUrl%-bin.*}-$distributionPlatform.zip"
;;
maven-mvnd-*) MVN_CMD=mvnd.sh _MVNW_REPO_PATTERN=/maven/mvnd/ ;;
-*) MVN_CMD="mvn${0##*/mvnw}" _MVNW_REPO_PATTERN=/org/apache/maven/ ;;
+*) MVN_CMD="mvn${scriptName#mvnw}" _MVNW_REPO_PATTERN=/org/apache/maven/ ;;
esac
# apply MVNW_REPOURL and calculate MAVEN_HOME
diff --git a/maven-wrapper-distribution/src/resources/only-mvnwDebug
b/maven-wrapper-distribution/src/resources/only-mvnwDebug
index 8e6da18..6c5bd98 100644
--- a/maven-wrapper-distribution/src/resources/only-mvnwDebug
+++ b/maven-wrapper-distribution/src/resources/only-mvnwDebug
@@ -21,4 +21,4 @@
# Apache Maven Wrapper debug script, version @@project.version@@
# -----------------------------------------------------------------------------
-. "${0%/*}"/mvnw
+. "$(dirname "$0")"/mvnw
diff --git a/maven-wrapper-distribution/src/resources/only-mvnwDebug
b/maven-wrapper-plugin/src/it/projects/sh_type_only-script/invoker.properties
similarity index 70%
copy from maven-wrapper-distribution/src/resources/only-mvnwDebug
copy to
maven-wrapper-plugin/src/it/projects/sh_type_only-script/invoker.properties
index 8e6da18..b9445ae 100644
--- a/maven-wrapper-distribution/src/resources/only-mvnwDebug
+++
b/maven-wrapper-plugin/src/it/projects/sh_type_only-script/invoker.properties
@@ -1,5 +1,3 @@
-#!/bin/sh
-
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
@@ -7,9 +5,9 @@
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -17,8 +15,5 @@
# specific language governing permissions and limitations
# under the License.
-# -----------------------------------------------------------------------------
-# Apache Maven Wrapper debug script, version @@project.version@@
-# -----------------------------------------------------------------------------
-
-. "${0%/*}"/mvnw
+# `/bin/sh` is not available by default on Windows systems
+invoker.os.family = !windows
diff --git a/maven-wrapper-plugin/src/it/projects/sh_type_only-script/pom.xml
b/maven-wrapper-plugin/src/it/projects/sh_type_only-script/pom.xml
new file mode 100644
index 0000000..07a7f5f
--- /dev/null
+++ b/maven-wrapper-plugin/src/it/projects/sh_type_only-script/pom.xml
@@ -0,0 +1,53 @@
+<?xml version='1.0' encoding='UTF-8'?>
+
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements. See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership. The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied. See the License for the
+specific language governing permissions and limitations
+under the License.
+-->
+
+<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">
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>org.apache.maven.plugins.it.wrapper</groupId>
+ <artifactId>extension</artifactId>
+ <version>1.0.0-SNAPSHOT</version>
+ <packaging>pom</packaging>
+
+ <build>
+ <pluginManagement>
+ <plugins>
+ <plugin>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>exec-maven-plugin</artifactId>
+ <version>3.0.0</version>
+ <configuration>
+ <executable>/bin/sh</executable>
+ <arguments>
+ <argument>mvnw</argument>
+ <argument>-v</argument>
+ </arguments>
+ <environmentVariables>
+ <MVNW_VERBOSE>true</MVNW_VERBOSE>
+ <HOME>${project.build.directory}</HOME>
+ <USERPROFILE>${project.build.directory}</USERPROFILE>
+ </environmentVariables>
+ </configuration>
+ </plugin>
+ </plugins>
+ </pluginManagement>
+ </build>
+</project>
diff --git a/maven-wrapper-distribution/src/resources/only-mvnwDebug
b/maven-wrapper-plugin/src/it/projects/sh_type_only-script/test.properties
similarity index 70%
copy from maven-wrapper-distribution/src/resources/only-mvnwDebug
copy to maven-wrapper-plugin/src/it/projects/sh_type_only-script/test.properties
index 8e6da18..f9a1960 100644
--- a/maven-wrapper-distribution/src/resources/only-mvnwDebug
+++ b/maven-wrapper-plugin/src/it/projects/sh_type_only-script/test.properties
@@ -1,5 +1,3 @@
-#!/bin/sh
-
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
@@ -7,9 +5,9 @@
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -17,8 +15,4 @@
# specific language governing permissions and limitations
# under the License.
-# -----------------------------------------------------------------------------
-# Apache Maven Wrapper debug script, version @@project.version@@
-# -----------------------------------------------------------------------------
-
-. "${0%/*}"/mvnw
+type=only-script
diff --git
a/maven-wrapper-plugin/src/it/projects/sh_type_only-script/verify.groovy
b/maven-wrapper-plugin/src/it/projects/sh_type_only-script/verify.groovy
new file mode 100644
index 0000000..c3001df
--- /dev/null
+++ b/maven-wrapper-plugin/src/it/projects/sh_type_only-script/verify.groovy
@@ -0,0 +1,43 @@
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+assert new File(basedir,'mvnw').exists()
+assert new File(basedir,'mvnw.cmd').exists()
+assert !(new File(basedir,'mvnwDebug').exists())
+assert !(new File(basedir,'mvnwDebug.cmd').exists())
+
+wrapperProperties = new File(basedir,'.mvn/wrapper/maven-wrapper.properties')
+assert wrapperProperties.exists()
+assert !wrapperProperties.text.contains('wrapperUrl')
+
+log = new File(basedir, 'build.log').text
+// check "mvn wrapper:wrapper" output
+assert log.contains('[INFO] Unpacked only-script type wrapper distribution
org.apache.maven.wrapper:maven-wrapper-distribution:zip:only-script:')
+
+// check "mvnw -v" output
+assert log.contains("Couldn't find ")
+assert log.contains(", downloading and installing it ...")
+
+Properties props = new Properties()
+new File(basedir,'.mvn/wrapper/maven-wrapper.properties').withInputStream {
+ props.load(it)
+}
+assert props.wrapperVersion.equals(wrapperCurrentVersion)
+assert props.distributionType.equals("only-script")