Debug School

rakesh kumar
rakesh kumar

Posted on

Gradle Wrapper

The Gradle wrapper allows us to run a build with a specified version and settings without the Gradle installation. This wrapper can be considered as a batch script on Windows and shell script for other OS. When we invoke the Gradle with the wrapper, it automatically downloads and builds the specified version.

Most of the software requires installation on a computer before use. Installation of any tool on a computer becomes an unnecessary burden for the users. It is also important to check that if the user has installed the right version or not. The Gradle wrapper overcomes all these problems and preferred by all the developers to start a Gradle build.

Gradle executes the build process that is independent of the installed version. So the wrapper is the preferred way for starting a Gradle build. We can create the wrapper script according to our needs.

Below figure demonstrate the workflow of Gradle:
Image description
We can find a gradlew file for UNIX based systems and gradlew.bat for Windows systems. These files act as gradle commands so, if Gradle is not installed on the machine, then it will be automatically downloaded and installed.

It is also possible to define a task that defines the version of the wrapper. If this task is executed, it creates the wrapper and downloads the correct version of Gradle.

Benefits of the Gradle Wrapper
Following are some benefits of using Gradle wrapper:

  1. Gradle wrapper standardizes a project on a specified Gradle version, and it leads to more reliable and robust builds.
  2. The Gradle wrapper provides the same Gradle version to different users, and the execution environment is as simple as changing the Wrapper definition.
    We are going to do the following things with a first Gradle wrapper:

  3. Set up a new Gradle project and add a wrapper to it.

  4. Run a project with an existing wrapper.

  5. Upgrade the wrapper to the latest version of Gradle.
    Adding the Gradle Wrapper
    Gradle comes with a built-in task called as a wrapper. When this task is executed, it generates the essential wrapper files in the project. To add the wrapper to a project, run the wrapper command as follows:

gradle wrapper
Enter fullscreen mode Exit fullscreen mode

Output:
Image description

Gradle Wrapper
The above command will provide the wrapper to our project and generate the wrapper properties file into directory gradle/wrapper/gradle-wrapper.properties.
Image description

Gradle Wrapper
The content of gradle-wrapper properties file is as follows:

distributionBase=GRADLE_USER_HOME  
distributionPath=wrapper/dists  
distributionUrl=https\://services.gradle.org/distributions/gradle-5.5.1-bin.zip  
zipStoreBase=GRADLE_USER_HOME 
Enter fullscreen mode Exit fullscreen mode

This file contains the below information about the Gradle distribution:

  1. The server that hosting the Gradle distribution.
  2. Types of Gradle distribution, by default, it is the bin distribution containing only the runtime but no sample code and documentation.
  3. The Gradle version is used for executing the build. By default, the wrapper task picks the installed version of the machine.
Note: If we want to share the wrapper files to other developers and execution environments, then we have to push them into version control. Mostly the wrapper files, including JAR files, are smaller in size. It is expected to add the JAR file to version control. However, some companies do not allow projects to submit binary files to version control. At that moment, there is no other option apart from this approach.
Enter fullscreen mode Exit fullscreen mode

All of the above aspects can be configured with the help of the command-line during wrapper files generation.

The below command is used to specify the Gradle version for downloading and executing the wrapper:

--gradle-version  
Enter fullscreen mode Exit fullscreen mode

Below command is used to specify the Gradle distribution type used for the wrapper. Available options are bin and all, and the default value is 'bin':


--distribution-type  
Enter fullscreen mode Exit fullscreen mode

Below command is used for pointing the full URL to the Gradle distribution zip files:

--gradle-distribution-url 
Enter fullscreen mode Exit fullscreen mode

Consider the following example to explain the use of the command-line options. We want to create the wrapper with version 6.1 and use the -all distribution to enable our IDE to enable code-completion and being able to navigate to the Gradle source code. The following command can fulfill these requirements:

$ gradle wrapper --gradle-version 6.1 --distribution-type al
Enter fullscreen mode Exit fullscreen mode

l

The above command will create a wrapper with version 6.1.

How to use the Gradle Wrapper
It is recommended to execute a build of the project with the wrapper to ensure a standard, control, and reliable execution of a build. Wrapper executes a build, almost like executing a build with a Gradle installation. We can either run gradlew or gradlew.bat command instead of the gradle command according to our operating system. The following command demonstrates how to use the wrapper on a Windows machine for a Java-based project:

gradlew.bat build

Output:
Image description

Top comments (0)