About Petri Kainulainen

Petri is passionate about software development and continuous improvement. He is specialized in software development with the Spring Framework and is the author of Spring Data book.

Getting Started with Gradle: Introduction

Gradle is a build tool which replaces XML based build scripts with an internal DSL which is based on Groovy programming language.

It has gained a lot of traction recently and that is why I decided to take a closer look at it.

This blog post is the first part of my Gradle tutorial, and it has two goals:

  1. to help us install Gradle
  2. to describe some of its basic concepts which helps us understand the future parts of this tutorial.

Let’s start by finding out how we can install Gradle.

Installing Gradle

If we are using Windows or Linux, we can install Gradle by following these steps:

  1. Download the binaries from the downloads page.
  2. Unpack the zip file and add the GRADLE_HOME/bin directory to the PATH environment variable.

If you run into problems during the installation, check out the official installation guide.

If we are using OS X, we can install Gradle by using Homebrew. We can do this by running the following command at command prompt:

brew install gradle

We can verify that Gradle is working properly by running the command gradle -v at command prompt. If Gradle is working properly, we should see the following output (Windows and Linux users will naturally see a bit different output):

> gradle -v

------------------------------------------------------------

Gradle 1.12

------------------------------------------------------------

Build time:   2014-04-29 09:24:31 UTC

Build number: none

Revision:     a831fa866d46cbee94e61a09af15f9dd95987421

Groovy:       1.8.6

Ant:          Apache Ant(TM) version 1.9.3 compiled on December 23 2013

Ivy:          2.2.0

JVM:          1.8.0 (Oracle Corporation 25.0-b70)

OS:           Mac OS X 10.9.3 x86_64

Let’s take a quick look at the basic concepts of a Gradle build.

A Short Introduction to Gradle Build

Gradle has two basic concepts: projects and tasks. These concepts are explained in the following:

  • A project is either something we build (e.g. a jar file) or do (deploy our application to production environment). A project consists of one or more tasks.
  • A task is an atomic unit work which is performed our build (e.g. compiling our project or running tests).

So, how are these concepts related to a Gradle build? Well, every Gradle build contains one or more projects.

The relationships between these concepts are illustrated in the following figure:

gradlebuild

We can configure our Gradle build by using the following configuration files:

  • The Gradle build script (build.gradle) specifies a project and its tasks.
  • The Gradle properties file (gradle.properties) is used to configure the properties of the build.
  • The Gradle Settings file (gradle.settings) is optional in a build which has only one project. If our Gradle build has more than one projects, it is mandatory because it describes which projects participate to our build. Every multi-project build must have a settings file in the root project of the project hierarchy.

You can get more information about Gradle build scripts by reading the Chapter 6. Build Scripts Basics of the Gradle User Guide.

Let’s move on and find out how we can add functionality to a Gradle build by using Gradle plugins.

Even Shorter Introduction to Gradle Plugins

The design philosophy of Gradle is that all useful features are provided by Gradle plugins. A Gradle plugin can:

  • Add new tasks to the project.
  • Provide a default configuration for the added tasks. The default configuration adds new conventions to the project (e.g. the location of source code files).
  • Add new properties which are used to override the default configuration of the plugin.
  • Add new dependencies to the project.

The User’s Manual of Gradle provides a list of standard Gradle plugins.

We can apply a Gradle plugin (this term is used when we add a plugin to a project) by using either its name or type.

We can apply a plugin by name (the name of the plugin is foo) by adding the following line to the build.gradle file:

apply plugin: 'foo'

On the other hand, if we want to apply a plugin by type (the type of the plugin is com.bar.foo), we must add the following line to the build.gradle file:

apply plugin: 'com.bar.foo'

You can get more information about applying plugins by reading the section 21.1 Applying Plugins of the Gradle User Guide.

That’s all for today. Let’s summarize what we learned from this blog post.

Summary

This blog post has taught us three things:

  • We learned how we can install Gradle.
  • We understood the basic building blocks of a Gradle build.
  • We know how we can add functionality to our build by using Gradle plugins.

The next part of this tutorial describes how we can create a simple Java project by using Gradle.

Reference: Getting Started with Gradle: Introduction from our JCG partner Petri Kainulainen at the Petri Kainulainen blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


three × = 12



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close