Sutthirak

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.

https://gradle.org

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/gradle.properties && echo "org.gradle.daemon=true" >> ~/.gradle/gradle.properties

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

build.gradle
├── gradle
│   └── wrapper
│        ├── gradle-wrapper.jar
│        └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
└── src
    ├── main
    │    └── java
    │	     └── Library.java
    │	
    └── test
         └── java
	     └── LibraryTest.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 {
    mavenCentral()
}

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;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(Application.class, 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;

@RestController
public class TestController {

    @RequestMapping("/")
    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

Success!