Sub topic 13
Reproducible Builds with mvnw and gradlew
Imagine you build your project with Maven 3.9.9. Your teammate has Maven 3.6.3. The CI server, a machine that builds your code automatically, has something else again. Each version can behave a little differently, so a build that works for you might fail for them. This chapter shows how a wrapper fixes that problem.
What a Wrapper Is
A wrapper is a small script you keep inside your project. When you run it, it checks which version of the build tool the project wants. If that version is not on your machine yet, the script downloads it. Then it runs the build with that exact version.
The two wrappers have short names:
mvnwis the Maven Wrapper. You type it in place ofmvn.gradlewis the Gradle Wrapper. You type it in place ofgradle.
A reproducible build is a build that gives the same result on every machine. A wrapper helps because everyone runs the same tool version. A new teammate does not even need to install Maven or Gradle. They only need a JDK.
How the Wrapper Works
The first run is slower because the script downloads the tool. Later runs reuse the saved copy.
The saved copies go into a folder in your home directory:
| Wrapper | Where the downloaded tool is kept |
|---|---|
| Maven Wrapper | `~/.m2/wrapper/dists` |
| Gradle Wrapper | `~/.gradle/wrapper/dists` |
Many projects on your machine can share one cached copy of the same version.
Creating the Wrappers
You create a wrapper once, with a tool you already have installed. After that, everyone uses the wrapper.
mvn wrapper:wrapper
./mvnw clean verify
gradle wrapper
./gradlew buildHere is what each line does:
mvn wrapper:wrapperruns thewrappergoal of the Maven Wrapper plugin. It adds the wrapper files to your project../mvnw clean verifybuilds the project through the wrapper. The./means "the file in this folder."gradle wrapperruns Gradle's built-inwrappertask. It adds the Gradle Wrapper files../gradlew buildbuilds the project through the Gradle Wrapper.
You can also choose the version on purpose:
mvn wrapper:wrapper -Dmaven=3.9.9
gradle wrapper --gradle-version 8.10.2The Files a Wrapper Adds
Each wrapper adds a few files. Here is what each one does.
| File | Tool | What it does |
|---|---|---|
| `mvnw` | Maven | Script for macOS and Linux |
| `mvnw.cmd` | Maven | Script for Windows |
| `.mvn/wrapper/maven-wrapper.properties` | Maven | Says which Maven version to download |
| `gradlew` | Gradle | Script for macOS and Linux |
| `gradlew.bat` | Gradle | Script for Windows |
| `gradle/wrapper/gradle-wrapper.jar` | Gradle | Small program that does the download |
| `gradle/wrapper/gradle-wrapper.properties` | Gradle | Says which Gradle version to download |
Newer versions of the Maven Wrapper plugin use only scripts by default, so you may not see a maven-wrapper.jar file. Older setups have one in .mvn/wrapper.
The properties files
A properties file is a plain text file of key=value lines. The most important key is distributionUrl. A distribution is the zip file that contains the build tool.
A typical Maven one looks like this:
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.9/apache-maven-3.9.9-bin.zipA typical Gradle one looks like this:
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/distsThe backslash before the colon in the Gradle file is normal. Properties files escape colons that way.
Commit the Wrapper to Version Control
Version control is a system such as Git that tracks changes to your files. The wrapper only helps if everyone gets it. So add all the wrapper files to Git:
git add mvnw mvnw.cmd .mvn
git add gradlew gradlew.bat gradle/wrapper
git commit -m "Add Maven and Gradle wrappers"The downloaded tool itself lives in your home folder, not in the project. You never commit that.
Running the Wrapper on Each System
The command is a little different on each operating system.
| System | Maven | Gradle |
|---|---|---|
| macOS or Linux | `./mvnw package` | `./gradlew build` |
| Windows Command Prompt | `mvnw.cmd package` | `gradlew.bat build` |
| Windows PowerShell | `.\mvnw.cmd package` | `.\gradlew.bat build` |
A common error: Permission denied
On macOS or Linux you may see Permission denied when you run ./mvnw. This means the file is not marked as executable, which means allowed to run as a program. Fix it like this:
chmod +x mvnw gradlew
git update-index --chmod=+x mvnw gradlewThe first line fixes your own copy. The second line tells Git to save the executable flag, so your teammates do not hit the same error.
Upgrading the Tool Version
To move the whole team to a new version, you change the wrapper and commit the change. Use the wrapper itself to do it:
./mvnw wrapper:wrapper -Dmaven=3.9.9
./gradlew wrapper --gradle-version 8.10.2These commands update the properties file. The Gradle docs suggest running the Gradle command a second time. The first run changes the version number. The second run uses the new version, so it can also update the scripts and the JAR to match. Then run a full build and commit the changed wrapper files. Everyone gets the new version the next time they pull.
Checking the Download Is Safe
The wrapper downloads a program from the internet and runs it. You can make it check that the file was not changed. A checksum is a long code computed from a file's bytes. If even one byte differs, the code is different.
Gradle publishes a SHA-256 checksum for each release on its website. You can pass it in like this:
./gradlew wrapper --gradle-version 8.10.2 --gradle-distribution-sha256-sum <checksum-from-gradle-website>This adds a distributionSha256Sum line to the properties file. Replace the placeholder with the real value from the Gradle releases page. The Maven Wrapper supports the same key in maven-wrapper.properties. If the downloaded file does not match, the wrapper stops instead of running it.
Quick Check: Which Version Am I Using?
Both wrappers can print their version:
./mvnw --version
./gradlew --versionCompare the output with the version in the properties file. They should match.
Exercise
Your teammate clones the project and runs ./gradlew build. They do not have Gradle installed. What happens?
Show answer
The gradlew script reads gradle/wrapper/gradle-wrapper.properties, downloads the Gradle version named in distributionUrl, saves it in ~/.gradle/wrapper/dists, and then runs the build. Your teammate only needs a JDK.
Exercise
You run ./mvnw package on Linux and see "Permission denied". What two commands fix it for you and for your team?
Show answer
Run chmod +x mvnw to make your copy executable, then git update-index --chmod=+x mvnw and commit, so Git stores the executable flag for everyone.
Exercise
A project's .gitignore contains the line *.jar. Why might ./gradlew fail for other developers, and how do you fix it?
Show answer
The rule keeps gradle/wrapper/gradle-wrapper.jar out of Git, and the Gradle scripts need that JAR to run. Add an exception line !gradle/wrapper/gradle-wrapper.jar to .gitignore, then add and commit the JAR.
Exercise
Does the wrapper make sure everyone uses Java 17? Explain.
Show answer
No. The wrapper only fixes the Maven or Gradle version. Java comes from JAVA_HOME or the PATH. To control the Java version in Gradle, use a toolchain block. In Maven, maven.compiler.release sets the bytecode target but does not install a JDK.