1. Overview

In this article, We are going explain how to create spring boot Gradle application or Spring boot Gradle Example. Gradle is very popular build tool as like maven. Here we have created spring boot Gradle application with Intellj IDE. Here is guild how to create java application with Gradle.

2. Example

Here in the example, we have gradlew.bat (for windows), gradlew(for Linux) and gradle\wrapper to build and run Gradle application which will be automatically created while creating Gradle application IDE. We can also add those files using the command line here is documentation for add wrapper files.

Spring boot gradle example

Spring boot Gradle example

2.1 build.gradle

Location of build.gradle must be at the root of the application that we can see in above project structure. Read comment  for each configuration requirements:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.9.RELEASE")  // spring boot gradle plugin will used for generate jar
    }
}

repositories {
    mavenCentral()
}

description = "Spring boot Gradle Example"

apply plugin: 'java'                                // plugin for java
apply plugin: 'org.springframework.boot'            // plugin for spring boot framework

jar {
    baseName = 'spring-boot-gradle-example'             // jar file name
    version =  '1.0.0'                                  // application version
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")     // web application dependency
}

sourceCompatibility = 1.8                               // for Java 1.8
targetCompatibility = 1.8

group 'spring-boot-demo'
version '1.0-SNAPSHOT'

2.2 application.properties

application.properties file contains properties related to spring boot application. make sure that application.properties file bust inside resources folder.

server.port=8181

2.3 SpringBootApplication

package com.javadeveloperzone;

import org.springframework.boot.SpringApplication;

@org.springframework.boot.autoconfigure.SpringBootApplication
public class SpringBootApplication {
    public static void main(String[] args){
        SpringApplication.run(SpringBootApplication.class);
    }
}

2.4 DemoController

Spring Controller which will handle a request for /hello

package com.javadeveloperzone;

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

@RestController
public class DemoController {
    @RequestMapping(value = "hello")
    public String hello(){
        return "Spring boot Gradle Example";
    }
}

Clean & Build Gradle Spring Boot Application

> gradlew clean build (It requires wrapper in the project like gradlew.cmd(Windows) and gradlew(Linux))

or

> gradle clean build (Gradle must be installed in the machine)

It will create jar file at \build\libs.

Starting a Gradle Daemon (subsequent builds will be faster)
:clean UP-TO-DATE
:compileJava
:processResources
:classes
:findMainClass
:jar
:bootRepackage
:assemble
:compileTestJava NO-SOURCE
:processTestResources NO-SOURCE
:testClasses UP-TO-DATE
:test NO-SOURCE
:check UP-TO-DATE
:build

Run Spring boot Application

> gradlew bootRun (It requires wrapper in the project like gradlew.cmd(Windows) and gradlew(Linux))

or

> gradle bootRun (Gradle must be installed in the machine)

:compileJava
<-------------> 0% EXECUTING
> IDLE
:processResources
:classes
:findMainClass
:bootRun

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.9.RELEASE)

2018-04-28 11:02:50.463  INFO 141760 --- [           main] c.j.SpringBootApplication                : Starting
 SpringBootApplication on Mahesh with PID 141760 (F:\extrawork\spring-boot\spring-boot-gradle-example\build\cl
asses\main started by Lenovo in F:\extrawork\spring-boot\spring-boot-gradle-example)
2018-04-28 11:02:54.576 INFO 141760 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8181 (http)
2018-04-28 11:02:54.587 INFO 141760 --- [ main] c.j.SpringBootApplication : Started SpringBootApplication in 4.65 seconds (JVM running for 5.202)

Our spring application is running on 8181 port, Let’s call web service:

http://localhost:8181/hello

Spring boot gradle example - output

Spring boot Gradle example – output

3. Conclusion

In this article, We have seen how can quick start with Spring boot Gradle Application with example.  Gradle is a replacement for Maven. Here is comparison of maven and Gradle.

4. References

5. Source Code

spring-boot-gradle-example (118 KB)

Was this post helpful?
Let us know, if you liked the post. Only in this way, we can improve us.
Yes
No

Leave a Reply

Your email address will not be published. Required fields are marked *