The Maven Wrapper (mvnw)
Your POM pins plugin versions, but not the version of Maven itself. A colleague on Maven 3.6, a CI agent on 3.9 and a laptop on 4.0 can build the same commit differently, or not at all. The Maven Wrapper fixes that: a small script committed with the project downloads (once) and runs exactly the Maven version the project asks for.
Generating the wrapper
The wrapper plugin writes the scripts. -Dmaven= chooses the Maven version they will use:
mkdir -p wrapped/src/main/java/demo && cd wrapped
cat > pom.xml <<'POM'
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>wrapped</artifactId>
<version>1.0</version>
<properties>
<maven.compiler.release>21</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
POM
echo 'package demo; public class App { public static void main(String[] a) { System.out.println("built with the wrapper"); } }' > src/main/java/demo/App.java
mvn -o wrapper:wrapper -Dmaven=3.9.16 | grep -E -- '--- wrapper|Unpacked|BUILD'
find . -path ./target -prune -o -type f -print | sort
cat .mvn/wrapper/maven-wrapper.properties
./mvnw -v | head -1
./mvnw -q package && java -cp target/classes demo.App
ls ~/.m2/wrapper/dists[INFO] --- wrapper:3.3.4:wrapper (default-cli) @ wrapped ---
[INFO] Unpacked only-script type wrapper distribution org.apache.maven.wrapper:maven-wrapper-distribution:zip:only-script:3.3.4
[INFO] BUILD SUCCESS
./.mvn/wrapper/maven-wrapper.properties
./mvnw
./mvnw.cmd
./pom.xml
./src/main/java/demo/App.java
wrapperVersion=3.3.4
distributionType=only-script
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.16/apache-maven-3.9.16-bin.zip
Apache Maven 3.9.16 (2bdd9fddda4b155ebf8000e807eb73fd829a51d5)
built with the wrapper
apache-maven-3.9.16
What was created:
| File | Purpose | Commit it? |
|---|---|---|
mvnw |
POSIX shell script | yes, with the executable bit |
mvnw.cmd |
Windows script | yes |
.mvn/wrapper/maven-wrapper.properties |
which Maven distribution to use | yes |
Since wrapper 3.3 the default type is only-script: there is no maven-wrapper.jar any more; the script itself downloads the distribution with curl/wget (or PowerShell on Windows) into ~/.m2/wrapper/dists/ and reuses it from then on. On this machine the 3.9.16 distribution was already there, so nothing was downloaded.
Using it
From now on, everyone runs ./mvnw instead of mvn, with the same arguments: ./mvnw verify, ./mvnw -pl app -am package. CI does the same, so the Maven version is part of the commit and changes through code review:
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.16/apache-maven-3.9.16-bin.zip
distributionSha256Sum=<sha-256 of that zip>Two lines worth adding in a real project:
distributionSha256Summakes the script verify the download. Without it, whoever controls the URL (or a network in between) controls your build tool.distributionUrlcan point at your repository manager instead of Maven Central, so builds work behind a firewall. Credentials for it come from theMVNW_USERNAME/MVNW_PASSWORDenvironment variables.
To upgrade Maven, run the plugin again with the new version (./mvnw wrapper:wrapper -Dmaven=…) and commit the changed properties file.
Tip: Keep
mvnwexecutable in Git (git update-index --chmod=+x mvnwif someone committed it from Windows). A CI job failing withPermission denied: ./mvnwis almost always this.
Test yourself
What problem does the Maven Wrapper solve?
- Slow downloads
- Different machines running different Maven versions on the same project
- Missing plugins
- Dependency conflicts
Which wrapper files belong in version control?
- None, they are generated
mvnw,mvnw.cmdand.mvn/wrapper/maven-wrapper.properties- Only
~/.m2/wrapper/dists - Only the Maven zip
Why add distributionSha256Sum to maven-wrapper.properties?
- To speed up the download
- So the wrapper verifies the downloaded Maven distribution before running it
- To pick a mirror
- It's required by Maven 3.9