Introduction to Gradle

Gradle Introduction

  • Gradle is full-flaged build tool for Java, Scala, Groovy and more.
  • Gradle requires a JDK 1.5 or higher.
  • Gradle ships with its own Groovy library, therefore no Groovy needs to be installed.
  • Any existing Groovy installation is ignored by Gradle.

Gradle Installation

Gradle releases come in three flavours:

- (binaries, sources and documentation)
- (binaries only)
- (sources only)

To download Gradle version 1.11

$ wget
For running Gradle, add GRADLE_HOME/bin to your PATH environment variable.
Usually, this is sufficient to run Gradle.

$ echo "export PATH=$PATH:$GRADLE_HOME/bin" >>~/.bashrc

To check if Gradle is properly installed just type

$ gradle -v

The output shows Gradle version and also local environment configuration (groovy and jvm version, etc.)

Build Script Basics

  • Everything in Gradle sits on top of two basic concepts: projects and tasks.
  • Every Gradle build is made up of one or more projects.
  • What a project represents depends on what it is that you are doing with Gradle.
    For example, a project might represent a library JAR or a web application or else things.
  • Each project is made up of one or more tasks.
  • A task represents some atomic piece of work which a build performs.
  • This might be compiling some classes, creating a JAR, generating javadoc, or publishing some
    archives to a repository.

You run a Gradle build using the gradle command.
The gradle command looks for a file called build.gradle in the current directory.
For example, the very simple build script may be like below


task hello {
    doLast {
        println 'Hello world!'

In a command-line shell, enter into the containing directory and
execute the build script by running

$ gradle -q hello
Hello world!

You can declare dependencies between your tasks.


task hello << {
    println 'Hello world!'
task intro(dependsOn: hello) << {
    println "I'm Gradle"

Gradle Plugins

Gradle comes with number plugins which provide some pre-configured tasks.
- Java Plugin
- Groovy Plugin
- Web Plugin
- Jetty Plugin
- and more....

Java Plugin

Gradle Java Plugin adds some pre-cofigured tasks to your build configuration script to compile,run unit tests and create jar etc.

To use the Java plugin, add the following to your build file:


apply plugin: 'java'

And this plugin adds following tasks

  • build
    Gradle will compile and test your code, and create a JAR file containing your main classes
    and resources
  • clean
    Deletes the build directory, removing all built files.
  • assemble
    Compiles and jars your code, but does not run the unit tests. Other plugins add more
    artifacts to this task. For example, if you use the War
    plugin, this task will also build the WAR file for your project.
  • check
    Compiles and tests your code. Other plugins add more checks to this task. For example, if you use the Code-quality plugin, this task will also
    run Checkstyle against your source code.

External dependencies

Usually, a Java project will have some dependencies on external JAR files.
To reference these JAR files in the project, you need to tell Gradle where to find them.
In Gradle, artifacts such as JAR files, are located in a repository.
A repository can be used for fetching the dependencies of a project, or for publishing the artifacts of a project, or both.
For example, we will use the public Maven repository:


repositories {

Now we will declare that our production classes have a compile-time dependency on commons collections,
and that our test classes have a compile-time dependency on junit:


dependencies {
    compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
    testCompile group: 'junit', name: 'junit', version: '4.+'

Multi-project Java build

For example following may be our multi-project layout


To define a multi-project build, you need to create a settings file and include following lines


include "shared", "api", "services:webservice", "services:shared"

Multi-project build - dependencies between projects

dependencies {
    compile project(':shared')

Obtaining information about build configuration

  • Listing Project: gradle -q projects
  • Listing Tasks: gradle -q tasks
  • Showing task usage: gradle -q help --task libs
  • Listing project dependencies: gradle -q dependencies api:dependencies webapp:dependencies
  • Listing project properties: gradle -q api:properties
  • Dry Run: gradle -m clean compile

Gradle Graphical User Interface

$ gradle --gui

Gradle Build Language

Gradle provides a domain specific language, or DSL, for describing builds.
This build language is based on Groovy, with some additions to make it easier to describe a build.

Build Init Plugin

The Gradle Build Init plugin can be used to bootstrap the process of creating a new Gradle build.
It supports creating brand new projects of different types as well as converting existing builds
(e.g. An Apache Maven build) to be Gradle builds.
The Build Init plugin is an automatically applied plugin,
which means you do not need to apply it explicitly to your project.

Build Init plugin - tasks

  • init :Generates a Gradle project.
  • wrapper :Generates Gradle wrapper files.

The init supports different build setup types.
The type is specified by supplying a --type argument value.

$ gradle init --type java-library [for java project]
$ gradle init --type pom [to convert existing maven project to gradle]
$ gradle init --type scala-library [for scala project]
$ gradle init --type groovy-library [for groovy project]
$ gradle init --type basic [for fresh new project]

If a --type parameter is not supplied, Gradle will attempt to infer the type from the environment.
For example, it will infer a type value of "pom" if it finds a pom.xml to convert to a Gradle build.
If the type could not be inferred, the type "basic" will be used.