The Gradle Wrapper
The wrapper is the reason a Gradle project builds with the same version on every machine. It is four files in the repository, written by one task, and ./gradlew is a script that reads them and runs the version they name.
gradle wrapper
settings.gradle
rootProject.name = 'shop'
build.gradle
// An empty build: the wrapper task is built in.
The task copies the wrapper of the running installation and writes the version it was asked for. It needs no network: the files come from the installation, and only the first ./gradlew downloads.
gradle wrapper --gradle-version 9.9.2 --console=plain --offline --no-daemon
> Task :wrapper
BUILD SUCCESSFUL in 12s
1 actionable task: 1 executed
The four files
ls -A1
.gradle
build.gradle
gradle
gradlew
gradlew.bat
settings.gradle
ls gradle/wrapper
gradle-wrapper.jar
gradle-wrapper.properties
gradle/wrapper/gradle-wrapper.properties
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-9.9.2-bin.zip
networkTimeout=10000
retries=0
retryBackOffMs=500
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
- gradlew is a POSIX shell script, and gradlew.bat its Windows twin. Both read the properties file, look for the distribution in ~/.gradle/wrapper/dists and download it when it is not there.
- gradle-wrapper.jar is the small program that does the download and the unpack. It is a binary of about 50 KB and it belongs in the repository; a .gitattributes marks it as such.
- gradle-wrapper.properties names the distribution. The URL is the whole version pin: change 9.9.2 and the next run downloads another Gradle.
./gradlew
./gradlew --version
That download is why the command is not run in this container: there is no network, and the distribution is not in this user’s ~/.gradle/wrapper/dists. On a machine with a network the first run prints the download progress, unpacks the zip and then prints the same --version block as gradle --version.
Everything else is the same as with the installed tool: every task of this course runs with ./gradlew in a project that has a wrapper, and with gradle in a folder that does not.
Upgrading the wrapper
The same task with another version is the upgrade. It rewrites the properties file and leaves the jar and the scripts as they are, and the change is one line in a diff, which is what a reviewer wants to see:
./gradlew wrapper --gradle-version 9.9.2 --distribution-type bin
The --distribution-type bin flag picks the distribution without the sources and the documentation, about 130 MB instead of 200. A team that wants to check the download adds distributionSha256Sum to the properties file, and Gradle refuses a zip whose checksum differs.
Related
- Install Gradle and check the version: the installation the wrapper replaces.
- Gradle in CI and Docker: the first ./gradlew of a pipeline.
- The Maven wrapper: the same idea with mvnw.
Автор статьи: Arthur Isaev