Search This Blog

Wednesday, 8 February 2017


Gradle is a build tool just like Maven.(if you are not familiar with maven check this link)

A build tool generally does provide features like dependency management, compilation, packaging & running the unit test cases during its build phases.

Build Tool Features: 

Dependency Management: Provides all the project dependencies (eg: some external jars)

Compilation: Compiles the source code

Packaging: packages the project in jar / war as you configure in build file

Test Cases Execution: once after successful compilation & before packaging it executes the test cases.
Maven uses pom.xml as its build file, where all dependencies of the project & packaging of the project (either jar/war) are declared. Whereas, Gradle uses build.gradle file to declare all the dependencies & plugins required for the project. Maven uses XML as its file format, while gradle uses Groovy code.

Note: But to start a gradle project, we don't need to be an expert in Groovy.

Gradle Installation:

you download the latest Gradle distribution here : Download Gradle

Note: Gradle require JDK / JRE 7 or higher versions (mandatory)

Once after downloading the distribution, add the bin folder path to environment variable "PATH". This makes the binary gradle commands to work any where in the command-line.

In Ubuntu: you can also install using this command

dinesh@dinesh:~$ sudo apt-get install gradle

(by using this above command, you do not need any extra configuration like maintaining PATH environment variable. The binary shell script directly placed under /usr/bin/ path, which is already by default added to PATH environment variable).

In Mac: you can also install using this command

dinesh@dinesh:~$ brew install gradle

I recommend to get the binary distribution directly from gradle site.

Now, you can test the installation using the following gradle command(in command-line)

dinesh@dinesh:~$ gradle -v 

for the successful installation, you will see the following output

Gradle 3.3

Build time:   2017-01-03 15:31:04 UTC
Revision:     075893a3d0798c0c1f322899b41ceca82e4e134b

Groovy:       2.4.7
Ant:          Apache Ant(TM) version 1.9.6 compiled on June 29 2015
JVM:          1.8.0_92 (Oracle Corporation 25.92-b14)
OS:           Linux 3.5.0-61-generic amd64

A Sample Gradle Project:

To create gradle project, you must first need to create build.gradle file

> Create a directory for the project

dinesh@dinesh:~$mkdir gradle_proj

> Switch to project

dinesh@dinesh:~$  cd gradle_proj

> Create a build.gradle file

dinesh@dinesh:~/gradle_proj$ gedit build.gradle

Note: gedit is an editor in ubuntu os, you can use any editor you are comfortable with, like sublime text, notepad++

Now, apply a java gradle plugin in the script: 

apply plugin: 'java' 

Note: This is important plugin inorder to resolve the project dependencies. Now, you can check the importance of the above line added in build.gradle file, you execute a gradle command

dinesh@dinesh:~/gradle_proj$ gradle tasks

This command "gradle tasks" shows all the project tasks.

A task is small atomic unit of work which performs our build (a task can be compilation, running tests. For that matter, any build tool feature).

when it is executed, it first search for the build.gradle, it shows all the tasks that are related to java plugin. For example, build & test are some of the tasks added. Some of the important tasks are assemble, build, test & dependencies

I can relate the tasks in gradle to Maven goals. 

when you run "gradle tasks", you may see these:

I recommend, once you execute the same command with empty build.gradle file to notice the tasks added especially by java gradle plugin.

Now, you can add a dependency, all the dependencies are added to dependencies block


To resolve, these dependencies you must also add a repository, where all dependencies are downloaded from.

Add a repository block


The added dependencies are successfully resolved by just executing this

dinesh@dinesh:~/gradle_proj$ gradle build

Now, this commands downloads all the dependencies from maven central. You can also use other repositories like jcenter().

Create a directory for adding java files:

dinesh@dinesh:~/gradle_proj$ mkdir -p src/main/java/sample

Add Some Source Code: Spring Boot Application

package sample;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;

public class SampleApp{ 

 public static void main(String[] args) throws Exception {, args);

 public String getHome(){
  return "I am Home";

To execute the spring boot application, I am adding the spring boot gradle plugin to classpath using buildscript block






To get the dependency to classpath, we need to separately define repositories block in buildscript block also.

And also apply the plugin

apply plugin: 'org.springframework.boot'

Now, execute the command 

dinesh@dinesh:~/gradle_proj$ gradle clean build

This once again builds. Once execute the ls command to see the files & directories (dir for windows). It creates build directory as maven creates a target directory.

dinesh@dinesh:~/gradle_proj$ gradle tasks

you will find a gradle task related to spring boot gradle plugin, bootRun:

 Now, you can start the spring boot application by running this gradle task: bootRun

dinesh@dinesh:~/gradle_proj$ gradle bootRun

This starts the spring boot application by searching the java file having main method:

  Now, type localhost:8080/home in your browser & it returns "I am Home".

                        Hope it helps :) Please share :)

No comments:

Post a Comment