Gradle Tutorial – Installation and Hello World Example

Gradle is build automation tool just like Maven or Ant – for writing build scripts to run them in any environment – on demand. In this tutorial, we will learn about how Gradle is different from other build automation tools and then we will see one simple example for installing and using gradle.

Table of Contents

Gradle vs. other build tools
Installing Gradle
Writing simple Gradle script
Executing Gradle script


Gradle vs. other build tools

If you have worked with maven, you know that you cannot generate more than one artifact (jar or war file etc.) from one build file (pom.xml). If you want to generate two jar files from signle src folder, then you will have to create possibly two projects in maven. This is one of the difficulties with existing build tools.

Other build tools, for years, had been used for compiling and packaging software. But with changing industry requirements, projects involve large and diverse software stacks, incorporate multiple programming languages, and apply a broad spectrum of testing strategies. With the rise of agile practices, builds have to support early integration of code as well as frequent and easy delivery to test and production environments.

Other tools fall short in meeting these goals and this where Gradle comes into picture. It draws on lessons learned from established tools and takes their best ideas to the next level.

Gradle’s ability to manage dependencies isn’t limited to external libraries. As your project grows in size and complexity, you’ll want to organize the code into modules with clearly defined responsibilities. Gradle provides powerful support for defining and organizing multiproject builds, as well as modeling dependencies between projects.

Gradle build scripts are declarative, readable, and clearly express their intention. Writing code in Groovy instead of XML, sprinkled with Gradle’s build-by-convention philosophy, significantly cuts down the size of a build script and is far more readable.

Installing Gradle

  1. As a prerequisite, make sure you’ve already installed the JDK 6 or higher.
  2. Getting started with Gradle is easy. You can download the distribution directly from the Gradle homepage at Distribution comes with source, binaries, documents and examples.
  3. Extract downloaded zip file at some place into your development machine. Now set the environment variable GRADLE_HOME to gradle directory.
  4. Add GRADLE_HOME/bin folder to PATH environment variable.

You should be able to run gradle -v command from command line. If it happens and you get the output, you are ready to use gradle into your project. Output of above command will look like this:

Gradle 2.14.1

Build time:   2016-07-18 06:38:37 UTC
Revision:     d9e2113d9fb05a5caabba61798bdb8dfdca83719

Groovy:       2.4.4
Ant:          Apache Ant(TM) version 1.9.6 compiled on June 29 2015
JVM:          1.6.0_33 (Sun Microsystems Inc. 20.8-b03)
OS:           Windows 7 6.1 x86

Writing simple Gradle script

Now let’s write a simle gradle script and try to run it. I have create new folder in desktop, and named in Playground. Then I have created build.gradle and written down below hello world script.

task helloWorld {
   doLast {
      println 'Hello world!'

Above script is written in Gradle’s DSL (self descriptive language) e.g. action named doLast is almost self-expressive. It’s the last action that’s executed for a task.

Executing Gradle script

To run the script, run the gradle command and pass the task name. Output will be as shown:

> gradle –q helloWorld
Hello world!

With option quiet with –q, you tell Gradle to only output the task’s output.

Above usecase very basic in nature, and has been written with intention to keep the things simple as much as it can – so you are familiar with most basic stuff first. In coming tutorials, we will learn the more advanced features of Gradle.

Happy Learning !!

Leave a Reply

Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial