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

./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

Автор статьи: Arthur Isaev

Изображение баннера