Sub topic 13
Reproducible Builds with mvnw and gradlew
Why the tool version matters
A build is reproducible when it gives the same result on every machine. Your code and your pom.xml or build.gradle.kts are the same everywhere. The build tool itself may not be. One teammate might have Maven 3.6 while another has Maven 3.9. A plugin can then behave differently, or a Gradle script can fail on an older Gradle.
A wrapper fixes this. A wrapper is a small script that lives in your project. It records the exact tool version the project needs. When you run it, it downloads that version if needed and then runs it. Nobody has to install Maven or Gradle by hand.
How a wrapper works
The first run downloads the tool. Later runs reuse the copy saved in your home folder.
Creating the wrappers
You need Maven or Gradle installed one time, just to create the wrapper. After that, everyone uses the wrapper.
mvn wrapper:wrapper
./mvnw clean verify
gradle wrapper
./gradlew buildThe mvn wrapper:wrapper command creates these files:
mvnwis the script for macOS and Linux.mvnw.cmdis the script for Windows..mvn/wrapper/maven-wrapper.propertiesstores the Maven version.
The gradle wrapper command creates these files:
gradlewandgradlew.batare the scripts for macOS/Linux and Windows.gradle/wrapper/gradle-wrapper.jaris a small program that does the download.gradle/wrapper/gradle-wrapper.propertiesstores the Gradle version.
To pin a specific version, pass it when you create the wrapper:
mvn wrapper:wrapper -Dmaven=3.9.6
gradle wrapper --gradle-version 8.8Using the wrapper every day
Once the wrapper exists, use it in place of the installed command:
| Instead of | Run on macOS/Linux | Run on Windows |
|---|---|---|
| `mvn package` | `./mvnw package` | `mvnw.cmd package` |
| `gradle build` | `./gradlew build` | `gradlew.bat build` |
The wrapper accepts the same phases, tasks, and options as the normal command. Your CI server should use it too. A CI server is a machine that builds and tests your code automatically. When CI runs the wrapper, it uses the same tool version as your laptop.
Upgrading later
To move to a newer version, run the wrapper command again with the new version, for example ./gradlew wrapper --gradle-version 8.10. Then commit the changed properties file. Everyone gets the upgrade the next time they pull your change.
Exercise
A teammate clones your Gradle project and runs ./gradlew build, but it fails because gradle/wrapper/gradle-wrapper.jar is missing. What went wrong, and how do you fix it?
Show answer
The wrapper JAR was never committed to version control, so the script cannot download Gradle. Run gradle wrapper (or ./gradlew wrapper on a machine where the wrapper works), then commit gradlew, gradlew.bat, and the whole gradle/wrapper/ folder.
Exercise
Which file would you open to see which Maven version a project's wrapper uses?
Show answer
.mvn/wrapper/maven-wrapper.properties. Its distributionUrl line contains the Maven version.