Book Image

Gradle Essentials

By : Kunal Dabir, Abhinandan Maheshwari
Book Image

Gradle Essentials

By: Kunal Dabir, Abhinandan Maheshwari

Overview of this book

Gradle is an advanced and modern build automation tool. It inherits the best elements of the past generation of build tools, but it also differs and innovates to bring terseness, elegance, simplicity, and the flexibility to build. Right from installing Gradle and writing your first build file to creating a fully-fledged multi-module project build, this book will guide you through its topics in a step-by-step fashion. You will get your hands dirty with a simple Java project built with Gradle and go on to build web applications that are run with Jetty or Tomcat. We take a unique approach towards explaining the DSL using the Gradle API, which makes the DSL more accessible and intuitive. All in all, this book is a concise guide to help you decipher the Gradle build files, covering the essential topics that are most useful in real-world projects. With every chapter, you will learn a new topic and be able to readily implement your build files.
Table of Contents (17 chapters)
Gradle Essentials
About the Authors
About the Reviewers

Installing Gradle

Before we move forward with running Gradle, we must have it installed on our machine. There are multiple ways through which Gradle can be installed and updated. We will first see a more manual way to install Gradle and then take a quick look at installing it via some commonly used package managers. We can choose any one method that fits the bill. Irrespective of the way we install Gradle, we must meet the following prerequisite.

Gradle needs Java Runtime Environment (JRE) 6 or Java Development Kit (JDK) 1.6 or higher. There is no other dependency. We recommend having JDK installed. To verify this, on the command line, we can check the Java version with the following command:

$ java -version 
java version "1.8.0"
Java(TM) SE Runtime Environment (build 1.8.0-b132)
Java HotSpot(TM) 64-Bit Server VM (build 25.0-b70, mixed mode)

If we don't see the output more or less like the one shown in the preceding command, there is problem with our JDK installation.


The latest JDK can be downloaded from the following URL:

Installing manually

If we want a finer control over the installation then this is a suitable route. This could be the case, when we cannot use the package managers, want very specific binaries to be downloaded and installed, or behind corporate firewalls where automatic downloading by package managers is not allowed. We need to download the Gradle binaries and make them available for use on the command line.

The latest Gradle distribution can be downloaded from As of writing the latest version is 2.9.

Gradle binary distribution comes in two flavors as follows:

  • This contains binaries, sources, and documentation

  • This contains binaries only

We can download any of the above depending on what we need. Also, this is an OS-independent zip so the same zip can be extracted on Mac OS X, Windows, and Linux. The next section makes the Gradle command available on the command line. This section is dependent on the OS we use.

Installing on Mac OS X and Linux

Let's say we extracted the downloaded zip as ~/gradle-2.9/. Now, we just need to add the following two lines at the end of .bashrc/, .bash_profile/, or .zshrc, depending on the OS and the shell that we use:

export GRADLE_HOME=~/gradle-2.9

Restart the terminal or source the modified file to have the change take effect.

Installing on Windows

Let's say we extracted the zip as C:\gradle-2.9, then perform the following steps:

  1. Open the Start menu, right click on Computer and select Properties.

  2. On Advanced system settings, select the Advanced tab, and then select Environment Variables....

  3. Click on New.

  4. Create a GRADLE_HOME environment variable with the value C:\gradle-2.9.


    Downloading the example code

    You can download the example code files from your account at for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.


    In future when we download the later version of Gradle, we would need to change on this value to point to the correct folder.

  5. Edit (or add if not already there) the PATH environment variable. At the end of its value, append ;%GRADLE_HOME%\bin (add a semicolon if multiple path entries exist).

Alternate methods of installing Gradle

Although the manual installation gives absolute control over the installation process, various tasks such as downloading and extracting the right version, upgrading to the latest versions, uninstalling, and editing environment variables quickly become cumbersome and error-prone. That is why many people prefer package managers to control the whole process.

Installing via OS-specific package managers

While installing manually, as mentioned in the previous section, is very easy, we can make it super-easy by using a package manager.

Some Linux distributions like Ubuntu ship with their package manager, Mac OS X, Windows don't have any package manager installed by default. However, luckily, there are multiple package managers available for both platforms. We will see the example of Homebrew on Mac and Chocolatey on Windows.

Mac OS X

Make sure we have Homebrew installed. If it is, installing Gradle is only a matter of using the following command:

$ brew install gradle


More details on Homebrew can be found at

Linux (Ubuntu)

Using the built in package manager on Ubuntu, which is called Advanced Packaging Tool (APT), we can install Gradle with the following command:

$ sudo apt-get install gradle

If we have Chocolatey installed, installing Gradle is just a command away:

c:\> cinst gradle


More details on Chocolatey can be found at

Installing via SDKMAN

SDKMAN stands for the Software Development Kit Manager. In its own words, the website describes it as: SDKMAN! is a tool for managing parallel versions of multiple Software Development Kits on most Unix based systems.

The advantage SDKMAN has over other package managers is that we can have multiple Gradle versions installed on a system and select a different version for a given project. If we have it installed, all we need to do is run following command:

$ sdk install gradle

SDKMAN can be installed from

Verifying the installation

In whichever way we choose to install Gradle, it's a good idea to verify that if it's working before we move ahead. We can do this by simply checking for Gradle's version on the command line:

$ gradle --version
Gradle 2.9

Build time:   2015-11-17 07:02:17 UTC
Build number: none
Revision:     b463d7980c40d44c4657dc80025275b84a29e31f

Groovy:       2.4.4
Ant:          Apache Ant(TM) version 1.9.3 compiled on December 23 2013
JVM:          1.8.0_25 (Oracle Corporation 25.25-b02)
OS:           Mac OS X 10.10.5 x86_64

If we see output similar to the above, we have Gradle installed correctly on our machine.


We can use -v instead --version to get the same result.

Setting JVM options

Although it's not required most of the time, but if in case we need to set some global options for the JVM that Gradle will use, Gradle provides us a convenient way to do that. We can set the GRADLE_OPTS environment variable with acceptable flags to tune the JVM.

Gradle also honors the JAVA_OPTS environment variable. However, we need to be careful when setting it, as this affects the setting for all the Java programs on a machine. Setting that we want to keep common for all the Java apps should be done via this variable and those that only need to be applied to Gradle should be set via GRADLE_OPTS.


Some commonly used options are -Xms and -Xmx, which set the minimum and maximum heap size of the JVM.