Jul 4, 2015

Actually I heard about Gradle long time ago and I know Android application use it as a building system. So I think it's quite interesting to study it. how can we use it for building a normal Java application.

First of all , You have to install it (off course). you can download it directly from their website.

In case you are using Ubuntu or another distribution. You can install it via package management command. Ubuntu will use this command

sudo apt-get install gradle

But this approach , you will get the old version of it. (When I tried I got 1.4 version but the current version is 2.4)

To make Gradle can build it more faster. You have to enable it as a daemon. Gradle can keep data and codes that possible to be used inside the memory. this will increase building performance a lot. You can enable it with this command. (This approach can be used for developer machine only. Not for the build server environment)

touch ~/.gradle/ && echo "org.gradle.daemon=true" >> ~/.gradle/

OK! everything is done. Next , we will create a new Java project with Gradle with this command

gradle init --type java-library

you will get the project files like this

├── gradle
│   └── wrapper
│        ├── gradle-wrapper.jar
│        └──
├── gradlew
├── gradlew.bat
├── settings.gradle
└── src
    ├── main
    │    └── java
    │	     └──
    └── test
         └── java

but let's focus on build.gradle This file will be used to describe every dependencies that we are going to use and how we build this application.

apply plugin: 'java'
apply plugin: 'application'

mainClassName = 'info.sutthirak.springboot.Application'

repositories {

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    compile 'org.springframework.boot:spring-boot-starter-web:1.2.5.RELEASE'
    testCompile 'junit:junit:4.12'

jar {
    baseName = 'sutthirak'
    version =  '0.1.0'

Next we will create a class for starting application and another class for serving REST. we will create the first one inside package info.sutthirak.springboot (same place that we already have declared in build.gradle). The code should look like this.

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

public class Application {

    public static void main(String[] args) {
        ApplicationContext ctx =, args);

And let's create a very simple REST controller. I created it inside package name controller.

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

public class TestController {

    public String index() {
        return "Greetings from Spring Boot!";

Now it's time for building it. we can do it with this command.

gradle build

and then start your application with this command

gradle run