Book Image

Gradle Effective Implementation Guide

Book Image

Gradle Effective Implementation Guide

Overview of this book

Gradle is the next generation in build automation. It uses convention-over-configuration to provide good defaults, but is also flexible enough to be usable in every situation you encounter in daily development. Build logic is described with a powerful DSL and empowers developers to create reusable and maintainable build logic."Gradle Effective Implementation Guide" is a great introduction and reference for using Gradle. The Gradle build language is explained with hands on code and practical applications. You learn how to apply Gradle in your Java, Scala or Groovy projects, integrate with your favorite IDE and how to integrate with well-known continuous integration servers.Start with the foundations and work your way through hands on examples to build your knowledge of Gradle to skyscraper heights. You will quickly learn the basics of Gradle, how to write tasks, work with files and how to use write build scripts using the Groovy DSL. Then as you develop you will be shown how to use Gradle for Java projects. Compile, package, test and deploy your applications with ease. When you've mastered the simple, move on to the sublime and integrate your code with continuous integration servers and IDEs. By the end of the "Gradle Effective Implementation Guide" you will be able to use Gradle in your daily development. Writing tasks, applying plugins and creating build logic will be second nature.
Table of Contents (20 chapters)
Gradle Effective Implementation Guide
About the Author
About the Reviewers

Getting started

In this section, we will download and install Gradle before writing our first Gradle build script.

Before we get and install Gradle, we must make sure we have a Java Development Kit(JDK) installed on our computer. Gradle requires JDK 5 or higher. Gradle will use the JDK found at the path set on our computer. We can check this by running the following command on the command line:

java -version

Although Gradle uses Groovy, we don't have to install Groovy ourselves. Gradle bundles the Groovy libraries with the distribution and will ignore a Groovy installation already available on our computer.

Gradle is available on the Gradle website, at From this page we can download the latest release of Gradle. Or, we can download a previous version if we want to. We can choose among three different distributions to download. We can download either the complete Gradle distribution, with binaries, sources, and documentation, or only the binaries, or only the sources.

To get started with Gradle, we download the standard distribution with the binaries, sources, and documentation. At the time of writing this book, the current release is 1.1. On computers with a Debian Linux operation sytem, we can install Gradle as a Debian package. On computers with Mac OS X, we can use MacPorts or Homebrow to install Gradle.

Installing Gradle

Gradle is packaged as a ZIP file for one of the three distributions. So, when we have downloaded the Gradle full distribution ZIP file, we must unzip the file. After unpacking the ZIP file we have the following:

  • Binaries in the bin directory

  • Documentation with the user guide, Groovy DSL, and the API documentation in the docs directory

  • A lot of samples in the samples directory

  • Source code for Gradle in the src directory

  • Supporting libraries for Gradle in the lib directory

  • A directory named init.d where we can store Gradle scripts that need to be executed each time we run Gradle

Once we have unpacked the Gradle distribution to a directory, we can open a command prompt. We change the directory to bin, which we extracted from the ZIP file. To check our installation, we run gradle -v and we get output, listing the JDK used and the library versions of Gradle:

$ gradle -v

Gradle 1.1

Gradle build time: Tuesday, July 31, 2012 1:24:32 PM UTC
Groovy: 1.8.6
Ant: Apache Ant(TM) version 1.8.4 compiled on May 22 2012
Ivy: 2.2.0
JVM: 1.6.0_33 (Apple Inc. 20.8-b03-424)
OS: Mac OS X 10.7.4 x86_64

Here we can check whether the displayed version is the same as the distribution version we have downloaded from the Gradle website.

To run Gradle on our computer we only have to add $GRADLE_HOME/bin to our PATH environment variable. Once we have done that, we can run the gradle command from every directory on our computer.

If we want to add JVM options to Gradle, we can use the environment variables JAVA_OPTS and GRADLE_OPTS. The former is a commonly used environment variable name to pass extra parameters to a Java application. Similarly, Gradle uses the GRADLE_OPTS environment variable to pass extra arguments to Gradle. Both environment variables are used so we can set them both with different values. This is mostly used to set, for example, an HTTP proxy or extra memory options.