Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Gradle Essentials

You're reading from   Gradle Essentials Master the fundamentals of Gradle using real-world projects with this quick and easy-to-read guide

Arrow left icon
Product type Paperback
Published in Dec 2015
Publisher
ISBN-13 9781783982363
Length 176 pages
Edition 1st Edition
Tools
Arrow right icon
Authors (2):
Arrow left icon
Abhinandan Maheshwari Abhinandan Maheshwari
Author Profile Icon Abhinandan Maheshwari
Abhinandan Maheshwari
Kunal Dabir Kunal Dabir
Author Profile Icon Kunal Dabir
Kunal Dabir
Arrow right icon
View More author details
Toc

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.

Note

The latest JDK can be downloaded from the following URL:

http://www.oracle.com/technetwork/java/javase/downloads/index.html

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 http://www.gradle.org/downloads. As of writing the latest version is 2.9.

Gradle binary distribution comes in two flavors as follows:

  • gradle-2.9-all.zip: This contains binaries, sources, and documentation
  • gradle-2.9-bin.zip: 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
export PATH=$PATH:$GRADLE_HOME/bin

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....
    Installing on Windows
  3. Click on New.
  4. Create a GRADLE_HOME environment variable with the value C:\gradle-2.9.

    Tip

    Downloading the example code

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

    Installing on Windows

    Tip

    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

Note

More details on Homebrew can be found at http://brew.sh.

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

Windows

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

c:\> cinst gradle

Note

More details on Chocolatey can be found at https://chocolatey.org.

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 http://sdkman.io/.

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.

Tip

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.

Tip

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

You have been reading a chapter from
Gradle Essentials
Published in: Dec 2015
Publisher:
ISBN-13: 9781783982363
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime