Learn more about the concepts of Gradle as a project building tool and also see how to configure and build a sample Java project.

WEBINAR:

On-Demand

Overview

Gradle is an automated project building tool that uses the concepts of both Apache Ant and Apache Maven but is based on a domain specific language rather than the traditional XML approach. Gradle is designed to support multi-project builds that are quite large and are difficult to manage via Ant or Maven.

This article will discuss the concepts of Gradle as a project building tool and also show how to configure and build a sample Java project.

Introduction

Gradle is an build tool that can be used to automate the process of building, testing, publishing, deploying, generating API documentations, etc.

Features: Gradle comes with following list of features:

Declarative build- Gradle provides declarative language elements that we can be assembled as per our choice. This declarative language is extensible which enables us to add our own new language or enhance the existing one. These elements also provide build by convention support for Java, Groovy and other modern Java based technologies.

Language for dependency based programming- The declarative language is very flexible and enables Gradle to support the specific requirements.

Structure the build- Gradle allows us to apply common design principles to our build, which enables us to create a structured build for our applications.

Scalability- Gradle has the ability to easily scale from a simple single project build to a huge enterprise multi-project build. It enables the incremental build and also has the ability to tackle the performance issues that plague large enterprise build scripts.

Multi-project support- Gradle supports multi-project builds. It enables us to maintain the relationships between different projects in the case of a multi-project build environment. It also supports partial builds. We can build a single subproject out of an enterprise application. While building the single subproject, Gradle takes care of the other subprojects if the said subproject has dependency on other subprojects.

Dependency Management- Gradle provides different ways to manage internal as well as external dependencies. It provides supports for all kinds of dependency management, starting from transitive dependency management with remote access to Maven or any other repository-- even the local file system.

Integration tool- Gradle can easily import any Ant project and its targets and converts them into native Gradle tasks at runtime. Gradle also provides conversion mechanism to convert the maven pom.xml file into Gradle script.

Migration- Gradle easily adapts to any structure. We can easily develop the Gradle build in the same production branch.

Groovy Support - Gradle scripts are written in Groovy and not XML based.

Gradle Architecture

The following diagram shows the architecture components of Gradle build tool.

Installation and Use

Gradle comes in a zip bundle and can be downloaded from http://www.gradle.org/downloads. On the website, you can find three downloadable files:

gradle-<VERSION>-all.zip - This bundle contains all components including binaries, source and documentation.

gradle-<VERSION>-bin.zip - This bundle contains only the binaries.

gradle-<VERSION>-src.zip - This bundle contains only the source files.

As a beginner, my advice is to download the gradle-<VERSION>-all.zip file. Unzip this bundle on your UNIX system and append the bin folder to the path.

Steps to Complete the Installation

: As a prerequisite install JDK 1.5 or higher. Groovy library comes with the Gradle bundle so no need to install Groovy separately.

Run Gradle via the gradle command. Check Gradle installation by using gradle -v command. It will display Gradle version and other details.

To test the installation, simply type 'gradle' on your terminal, you should see the following output:

Listing 1: Sample showing testing the installation

: help
Welcome to Gradle <version>.
To run a build, run gradle <task>...
To see a list of available tasks, run gradle tasks
To see a list of command-line options, run gradle --help
BUILD SUCCESSFUL
Total time: 3.135 secs

Once we are done with the installation, to check this, let us create a sample java project that has a simple java class:

The gradle file above is a very basic one. Now if we run the 'gradle task' command again, we can see a list of tasks available to build the project, create javadocs etc. Here we will use the 'gradle build' task more frequently. This task is responsible for:

Compiling the source code

Running the test classes

Assembling the code in a jar or a war file.

Once the build task is executed, and we see 'BUILD SUCCESSFUL' message we can see several folders being added inside the build folder. The most common ones are:

libs - This contains the assembled project libraries (usually jar or war files).

Now let's go inside the above script

The first couple of lines mentions the plugins that we need during the build process.

Next part is the repositories. This is the location where we include the third party libraries. Once declared, we are ready for the third party libraries. Here we are using the joda time library version 2.2 of the joda time group. This is explained in the dependencies block. Other important dependencies are:

providedCompile - This includes the dependencies used for compiling the project.

testCompile - This includes the dependencies used for compiling and running the test cases of a project.

In the last block we specify the name of our jar file along with its version.

About the Author

Kaushik Pal is a technical architect with 15 years of experience in enterprise application and product development. He has expertise in web technologies, architecture/design, java/j2ee, Open source and big data technologies. You can find more of his work at www.techalpine.com and you can email him here.