The Gradle Wrapper
The Gradle Wrapper was the model for the Maven Wrapper, and in the Gradle world it is universal: almost no project expects you to have Gradle installed. You run ./gradlew, which downloads the Gradle version written in gradle/wrapper/gradle-wrapper.properties (once, into ~/.gradle/wrapper/dists/) and runs the build with it.
Creating and using it
The wrapper task, available in every build, writes the files. Without options it pins the Gradle version that runs it:
mkdir -p w/src/main/java/demo && cd w
echo 'rootProject.name = "w"' > settings.gradle.kts
echo 'plugins { java }' > build.gradle.kts
echo 'package demo; public class A { }' > src/main/java/demo/A.java
gradle wrapper -q
find . -type f -not -path './.gradle/*' -not -path './build/*' | sort
cat gradle/wrapper/gradle-wrapper.properties
./gradlew --version | grep '^Gradle'
./gradlew -q build && ls build/libs./build.gradle.kts
./gradle/wrapper/gradle-wrapper.jar
./gradle/wrapper/gradle-wrapper.properties
./gradlew
./gradlew.bat
./settings.gradle.kts
./src/main/java/demo/A.java
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-9.8.2-bin.zip
networkTimeout=10000
retries=0
retryBackOffMs=500
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Gradle 9.8.2
w.jar
| File | Commit it? | Purpose |
|---|---|---|
gradlew, gradlew.bat |
yes (keep gradlew executable) |
the launcher scripts |
gradle/wrapper/gradle-wrapper.jar |
yes | the small program that downloads and starts Gradle |
gradle/wrapper/gradle-wrapper.properties |
yes | which distribution to use, and where to store it |
Unlike the Maven Wrapper's script-only mode, the Gradle Wrapper needs its JAR committed: a fresh clone must be able to run ./gradlew with nothing but a JDK installed.
Upgrading and verifying
To change the version, run ./gradlew wrapper --gradle-version 9.9.0. With an explicit version, Gradle first checks that the distribution URL exists (validateDistributionUrl), which needs the network, so on this offline machine it fails. --no-validate-url skips the check; the session uses it to re-pin 9.8.2 together with a checksum:
mkdir w && cd w
echo 'rootProject.name = "w"' > settings.gradle.kts
gradle wrapper --gradle-version 9.8.2 2>&1 | grep '^> Test of'
gradle wrapper -q --gradle-version 9.8.2 --no-validate-url \
--gradle-distribution-sha256-sum 21bd5da2de49b642904077f38c0efdc7778e92b3ed0889392dca61b97e389607
grep -E 'distribution(Url|Sha256Sum)' gradle/wrapper/gradle-wrapper.properties
./gradlew -q help --task wrapper | grep -E '^ +--(gradle-version|gradle-distribution-sha256-sum|no-validate-url) '> Test of distribution url https://services.gradle.org/distributions/gradle-9.8.2-bin.zip failed. Please check the values set with --gradle-distribution-url and --gradle-version.
distributionSha256Sum=21bd5da2de49b642904077f38c0efdc7778e92b3ed0889392dca61b97e389607
distributionUrl=https\://services.gradle.org/distributions/gradle-9.8.2-bin.zip
--gradle-distribution-sha256-sum The SHA-256 hash sum of the gradle distribution.
--gradle-version The version of the Gradle distribution required by the wrapper. The following labels are allowed: latest, release-candidate, release-milestone, release-nightly, and nightly.
--no-validate-url Disables option --validate-url.
With distributionSha256Sum set, the wrapper refuses a downloaded ZIP whose hash differs. Gradle publishes the checksums next to each release; take the value from there, not from a ZIP you downloaded yourself.
Wrapper hygiene
- Commit all four files, and review changes to
gradle-wrapper.jarlike code. The officialgradle/actions/wrapper-validationGitHub Action checks the JAR against Gradle's known checksums, because a modified wrapper JAR runs on every developer's machine. - Upgrade by running the task twice:
./gradlew wrapper --gradle-version Xupdates the properties, and a second./gradlew wrapperlets the new Gradle regenerate the scripts and JAR. - Point
distributionUrlat your repository manager when builds must not reach the internet. - Use
-bindistributions (asgradle initdoes);-alladds Gradle's sources and docs, which only some IDE features need.
Test yourself
Which wrapper file must be committed for ./gradlew to work on a fresh clone with only a JDK?
- Only gradlew
- gradle/wrapper/gradle-wrapper.jar (with the scripts and properties)
- ~/.gradle/wrapper/dists
- build.gradle.kts
What does distributionSha256Sum protect against?
- Slow downloads
- Running a tampered or substituted Gradle distribution
- Version conflicts
- Daemon crashes
Why validate gradle-wrapper.jar in CI (e.g. with gradle/actions/wrapper-validation)?
- It changes on every build
- A malicious JAR in a pull request would run on every machine that builds the project
- To speed up builds
- Gradle requires it