The Gradle Wrapper
The recommended way to execute any Gradle build is with the help of the Gradle Wrapper (in short just “Wrapper”). The Wrapper is a script that invokes a declared version of Gradle, downloading it beforehand if necessary. As a result, developers can get up and running with a Gradle project quickly without having to follow manual installation processes saving your company time and money.
In a nutshell you gain the following benefits:
- Standardizes a project on a given Gradle version, leading to more reliable and robust builds.
- Provisioning a new Gradle version to different users and execution environment (e.g. IDEs or Continuous Integration servers) is as simple as changing the Wrapper definition.
So how does it work? For a user there are typically three different workflows:
- You set up a new Gradle project and want to add the Wrapper to it.
- You want to run a project with the Wrapper that already provides it.
- You want to upgrade the Wrapper to a new version of Gradle.
The following sections explain each of these use cases in more detail.
Adding the Gradle Wrapper
Generating the Wrapper files requires an installed version of the Gradle runtime on your machine as described in Installation. Thankfully, generating the initial Wrapper files is a one-time process.
Every vanilla Gradle build comes with a built-in task called wrapper . You’ll be able to find the task listed under the group «Build Setup tasks» when listing the tasks. Executing the wrapper task generates the necessary Wrapper files in the project directory.
$ gradle wrapper > Task :wrapper BUILD SUCCESSFUL in 0s 1 actionable task: 1 executed
To make the Wrapper files available to other developers and execution environments you’ll need to check them into version control. All Wrapper files including the JAR file are very small in size. Adding the JAR file to version control is expected. Some organizations do not allow projects to submit binary files to version control. At the moment there are no alternative options to the approach.
The generated Wrapper properties file, gradle/wrapper/gradle-wrapper.properties , stores the information about the Gradle distribution.
- The server hosting the Gradle distribution.
- The type of Gradle distribution. By default that’s the -bin distribution containing only the runtime but no sample code and documentation.
- The Gradle version used for executing the build. By default the wrapper task picks the exact same Gradle version that was used to generate the Wrapper files.
- Optionally, a timeout in ms used when downloading the gradle distribution.
- Optionally, a boolean to set the validation of the distribution url.
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2.1-bin.zip
All of those aspects are configurable at the time of generating the Wrapper files with the help of the following command line options.
The Gradle version used for downloading and executing the Wrapper. The resulting distribution url is validated before it is written to the properties file.
The following labels are allowed:
The Gradle distribution type used for the Wrapper. Available options are bin and all . The default value is bin .
The full URL pointing to Gradle distribution ZIP file. Using this option makes —gradle-version and —distribution-type obsolete as the URL already contains this information. This option is extremely valuable if you want to host the Gradle distribution inside your company’s network. The url is validated before it is written to the properties file.
The network timeout to use when downloading the gradle distribution, in ms. The default value is 10000 .
Disables the validation of the configured distribution url.
Enables the validation of the configured distribution url. Enabled by default.
If the distribution url is configured with —gradle-version or —gradle-distribution-url , the url is validated by sending a HEAD request in the case of the https scheme or by checking the existence of the file in the case of the file scheme.
Let’s assume the following use case to illustrate the use of the command line options. You would like to generate the Wrapper with version 8.2.1 and use the -all distribution to enable your IDE to enable code-completion and being able to navigate to the Gradle source code. Those requirements are captured by the following command line execution:
$ gradle wrapper --gradle-version 8.2.1 --distribution-type all > Task :wrapper BUILD SUCCESSFUL in 0s 1 actionable task: 1 executed
As a result you can find the desired information in the Wrapper properties file.
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2.1-all.zip
Let’s have a look at the following project layout to illustrate the expected Wrapper files:
. ├── a-subproject │ └── build.gradle.kts ├── settings.gradle.kts ├── gradle │ └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew └── gradlew.bat
. ├── a-subproject │ └── build.gradle ├── settings.gradle ├── gradle │ └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew └── gradlew.bat
A Gradle project typically provides a settings.gradle(.kts) file and one build.gradle(.kts) file for each subproject. The Wrapper files live alongside in the gradle directory and the root directory of the project. The following list explains their purpose.
The Wrapper JAR file containing code for downloading the Gradle distribution.
A properties file responsible for configuring the Wrapper runtime behavior e.g. the Gradle version compatible with this version. Note that more generic settings, like configuring the Wrapper to use a proxy, need to go into a different file.
A shell script and a Windows batch script for executing the build with the Wrapper.
You can go ahead and execute the build with the Wrapper without having to install the Gradle runtime. If the project you are working on does not contain those Wrapper files then you’ll need to generate them.
Using the Gradle Wrapper
It is recommended to always execute a build with the Wrapper to ensure a reliable, controlled and standardized execution of the build. Using the Wrapper looks almost exactly like running the build with a Gradle installation. Depending on the operating system you either run gradlew or gradlew.bat instead of the gradle command. The following console output demonstrate the use of the Wrapper on a Windows machine for a Java-based project.
$ gradlew.bat build Downloading https://services.gradle.org/distributions/gradle-5.0-all.zip . Unzipping C:\Documents and Settings\Claudia\.gradle\wrapper\dists\gradle-5.0-all\ac27o8rbd0ic8ih41or9l32mv\gradle-5.0-all.zip to C:\Documents and Settings\Claudia\.gradle\wrapper\dists\gradle-5.0-al\ac27o8rbd0ic8ih41or9l32mv Set executable permissions for: C:\Documents and Settings\Claudia\.gradle\wrapper\dists\gradle-5.0-all\ac27o8rbd0ic8ih41or9l32mv\gradle-5.0\bin\gradle BUILD SUCCESSFUL in 12s 1 actionable task: 1 executed
In case the Gradle distribution is not available on the machine, the Wrapper will download it and store in the local file system. Any subsequent build invocation is going to reuse the existing local distribution as long as the distribution URL in the Gradle properties doesn’t change.
The Wrapper shell script and batch file reside in the root directory of a single or multi-project Gradle build. You will need to reference the correct path to those files in case you want to execute the build from a subproject directory e.g. ../../gradlew tasks .
Upgrading the Gradle Wrapper
Projects will typically want to keep up with the times and upgrade their Gradle version to benefit from new features and improvements. One way to upgrade the Gradle version is manually change the distributionUrl property in the Wrapper’s gradle-wrapper.properties file. The better and recommended option is to run the wrapper task and provide the target Gradle version as described in Adding the Gradle Wrapper. Using the wrapper task ensures that any optimizations made to the Wrapper shell script or batch file with that specific Gradle version are applied to the project. As usual, you should commit the changes to the Wrapper files to version control.
Note that running the wrapper task once will update gradle-wrapper.properties only, but leave the wrapper itself in gradle-wrapper.jar untouched. This is usually fine as new versions of Gradle can be run even with ancient wrapper files. If you nevertheless want all the wrapper files to be completely up-to-date, you’ll need to run the wrapper task a second time.
Use the Gradle wrapper task to generate the wrapper, specifying a version. The default is the current version. Once you have upgraded the wrapper, you can check that it’s the version you expect by executing ./gradlew —version .
$ ./gradlew wrapper --gradle-version latest BUILD SUCCESSFUL in 4s 1 actionable task: 1 executed
$ ./gradlew wrapper --gradle-version 8.2.1 BUILD SUCCESSFUL in 4s 1 actionable task: 1 executed
Customizing the Gradle Wrapper
Most users of Gradle are happy with the default runtime behavior of the Wrapper. However, organizational policies, security constraints or personal preferences might require you to dive deeper into customizing the Wrapper. Thankfully, the built-in wrapper task exposes numerous options to bend the runtime behavior to your needs. Most configuration options are exposed by the underlying task type Wrapper.
Let’s assume you grew tired of defining the -all distribution type on the command line every time you upgrade the Wrapper. You can save yourself some keyboard strokes by re-configuring the wrapper task.