Chapter 16
The Gradle Wrapper

Gradle is a new tool. You can’t expect it to be installed on machines beyond your sphere of influence. An example are continuous integration server where Gradle is not installed and you have no admin rights for the machine. Or what if you provide an open source project and you want to make it as easy as possible for your users to build it.

There is a simple and good news. Gradle provides a solution for this. It ships with a Wrapper task.1 2 You can create such a task in your build script.

  createTask('wrapper', type: Wrapper).configure {
      gradleVersion = '0.1'
  }

This task is usually explicitly executed (for example after a switch to a new version of Gradle). After such an execution you find the following new or updated files in your project folder.

  project-root
    - gradle-wrapper
       - gradle-wrapper.jar
    - gradlew
    - gradlew.bat

All these files should be submitted to your version control system. The gradlew commands can be used exactly the same way as the gradle commands. If you run Gradle with gradlew, Gradle looks if the directory project-root/gradle-wrapper/gradle-dist/gradle-0.1 exists. If it exists, the gradle command of this distribution is called with all the arguments passed to the gradlew command. If the distribution directory does not exists, Gradle looks if the file project-root/gradle-wrapper/gradle-dist/gradle-0.1.zip does exists. If so, the zip is unpacked and gradle is called. If the distribution zip does not exists, Gradle tries to download it. You can specify the download url root via the urlRoot property of the Wrapper task. If you don’t specify it, Gradle uses http://dist.codehaus.org/gradle as its value.

If you don’t want any download to happen when your project is build via gradlew, simply add the Gradle distribution zip to your version control at the location described above. If you build via the wrapper, any existing Gradle distribution installed on the machine is ignored.

16.1 Unix file permissions

The Wrapper task adds appropriate file permissions to allow the execution for the gradlew *NIX command. Subversion preserves this file permission. We are not sure how other version control systems deal with this. What should always work is to execute sh gradlew.

16.2 Environment variable

Some rather exotic use cases might occur when working with the Gradle Wrapper. For example the continuos integration server goes down during unzipping the Gradle distribution. As the distribution directory exists gradlew delegates to it but the distribution is corrupt. Or the zip-distribution was not properly downloaded. When you have no admin right on the continuous integration server to remove the corrupt files, Gradle offers a solution via environment variables.

Variable Name

Meaning
GRADLE_WRAPPER_ALWAYS_UNPACK

If set to true, the distribution directory gets always deleted when gradlew is run and the distribution zip is freshly unpacked. If the zip is not there, Gradle tries to download it.

GRADLE_WRAPPER_ALWAYS_DOWNLOAD

If set to true, the distribution directory and the distribution zip gets always deleted when gradlew is run and the distribution zip is freshly downloaded.