Using and Adding Gradlew, The Gradle Wrapper for Java Projects

This tutorial is going to cover shortly about Gradlew – the Gradle Wrapper, how to use it, and how to create it for our projects.

Gradlew - The Gradle Wrapper

Gradle

1. Introduce about the Gradle Wrapper

Firstly, let’s review what will we have to do when we have a Gradle project

  • Download and install Gradle if we didn’t have it –> May take time and install not right version for the build.
  • Use the existing version if we had it –> The existing may be not the right version for the the build.

Gradle provides Gradlew, the Gradle Wrapper, will help us resolve the above problems.

So, what will Gradle Wrapper do?

Generally, it will:

  • Allow us to define the desire version of Gradle in configuration file.
  • When users use the Wrapper to build, it’ll check for the right version of Gradle in user machine. If not found, it will download Gradle automatically and use for the build.

2. Use Gradle Wrapper to build a project

To use Gradle Wrapper to build a project, we need to make sure it was added to the project first. We can do that by looking for the gradlew and related in the root project directory.

Let’s assume that the project has been set up the wrapper, we can execute the build using the following command:

On Linux and Mac OSX:

On Windows

For example:

If the desire version of Gradle is not found, the Wrapper will download Gradle automatically at the first time and then cache it in the local for the next times. You can refer to the following article for Gradle Cache Location

3. Add Gradle Wrapper to a project

There are 2 approaches for us to add Gradle Wrapper to a project.

3.1. Running the wrapper task

Here is the output of the above command:
Note that this task is always available, even if you don’t add it to your build. This task will configure the wrapper so that it will use the latest version of Gradle.

To specify a Gradle version, we can use the –gradle-version option as below:

3.2. Defining the wrapper task in the build.gradle

Add the below task in the build.gradle file.

Then we execute the task:

We will get the same result likes the first approach.

3.3. TheĀ  wrapper generated files

After we run the Gradle command to add the wrapper to our project, there are files generated in the project directory

Those files are used for Wrapper and should be kept in the project and checked in source control system also.

4. Summary

We have learned about to use Gradle Wrapper to build a project and how to add Gradle Wrapper to a project as well . Even it takes a little bit time at the first time for downloading the Gralde, it’s simplify the build for all Gradle project in general. Below are other articles related to Gradle for your reference:

Install Gradle on Ubuntu 16.04 LTS (Xenial Xerus)

Install Gradle on Windows

Where is Gradle Cache Location

Gradle Proxy Settings

Set Java Source Compatibility and Target Compatibility in Gradle

Refresh or Redownload Dependencies in Gradle

Gradle Tutorial