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 I why I decided to take a closer look at it.

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

  1. help us to install Gradle
  2. describe some of its basic concepts which helps us to 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 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 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 (settings.gradle) 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.

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.

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'

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 understand 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.

If you want to learn how to use Gradle, you should take a look at my Gradle tutorial.
22 comments… add one
  • cc Jun 15, 2014 @ 7:18

    I strongly recommend you take a closer look at gradle

    • Petri Jun 15, 2014 @ 11:51

      I will do that.

  • Powerfule Jun 23, 2014 @ 16:52

    Good post! Will you update more articles about Gradle?

    • Petri Jun 23, 2014 @ 17:00

      Thanks! I really appreciate your kind words.

      And yes, I am going to write a Gradle tutorial which covers the essential parts of Gradle. This tutorial follows the same format as my other tutorials, and the idea is that after this tutorial is over, you should be able to use Gradle in real life software projects.

  • Grendel Nov 24, 2014 @ 15:00

    Nice format. It's really helpful to those who just want a big picture of gradle without being overwhelmed by details. Keep it up sir.

    • Petri Nov 24, 2014 @ 19:59

      Thank you for your kind words. I really appreciate them.

  • Indian Dec 16, 2014 @ 12:27

    Nice Post. Could you please share list of Gradle Commands?
    Thanks.

    • Petri Dec 16, 2014 @ 20:36

      I am happy to hear that you liked this tutorial.

      Could you please share list of Gradle Commands?

      If you want to compile a Java project by using Gradle, you should read the second part of my Gradle tutorial. It describes how you can create a simple Java project and explains the basic tasks that can be invoked after you have applied the Gradle Java plugin to your build.

      Also, if you need to figure out the tasks of an existing Gradle project, you should run the command: gradle tasks.

      I hope that this answered to your question.

  • Fareed Mohammed Mar 10, 2015 @ 0:33

    you have explain very well . Thanks a lot .

    • Petri Mar 11, 2015 @ 19:43

      You are welcome!

  • Shaik Asif Basha Oct 7, 2015 @ 14:23

    I really appreciate the effort that u have put to explain, and it really helped me.
    Thanks

    • Petri Oct 8, 2015 @ 10:35

      Thank you for your kind words. I really appreciate them!

  • Sharath Nov 14, 2015 @ 2:27

    Short and sweet... Thanks for sharing awesome blog post...

    • Petri Nov 14, 2015 @ 10:10

      You are welcome. It is nice to hear that this blog post was useful to you.

  • Andrii Mar 13, 2017 @ 16:07

    You have a typo:
    > The Gradle Settings file (gradle.settings)
    should be
    > The Gradle Settings file (settings.gradle)

    • Petri Mar 13, 2017 @ 19:37

      Oops. Thank you for pointing this out. It is fixed now.

  • Bilal May 2, 2018 @ 5:19

    Good research, written short. Well done!

    • Petri May 2, 2018 @ 21:07

      Thank you for your kind words. I really appreciate them.

Leave a Reply