Spring Boot Gradle Example (2024)
In this tutorial, we'll show you how to make a spring boot Gradle application or a Spring boot Gradle example. Gradle, is like Maven, widely used build tool. With the Intellj IDE, we created a spring boot Gradle application.
Let's create Gradle Project from Spring Initializer site https://start.spring.io/
OR, we can create Gradle Project from Intellij IDE - Select File -> New Project and select group,
artifactId, name.
Project Structure
The files listed below will be created automatically in the gradle project.
gradlew.bat
file for windowsgradlew
file for Linuxgradle\wrapper
file to build and run Gradle application
build.gradle
build.gradle
must be located at the root of the application.
buildscript {
repositories {
mavenCentral()
}
dependencies {
// For generating jars, the spring boot gradle plugin will be utilised.
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.9.RELEASE")
}
}
repositories {
mavenCentral()
}
description = "Demo project for Spring Boot Gradle Example"
apply plugin: 'java' // java plugin
apply plugin: 'org.springframework.boot' // spring boot framework plugin
jar {
baseName = 'spring-boot-gradle-example' // jar file name
version = '1.0.0' // application version
}
dependencies {
// spring boot web application dependency
compile("org.springframework.boot:spring-boot-starter-web")
}
sourceCompatibility = 1.8 // Java 1.8
targetCompatibility = 1.8
group 'spring-boot-demo'
version '1.0-SNAPSHOT'
Take a look at our suggested posts:
Application Properties
application.properties
will have properties related to spring boot application. Now we
have made application server port to 8082
.
server.port=8082
Rest Controller
Create controller class to test our gradle application.
package com.techgeekenext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestGradleController {
@GetMapping(value = "/welcome")
public String hello() {
return "TechGeekNext - Welcome to Spring boot Gradle Demo";
}
}
Gradle Commands
- Clean and Build the Gradle Application :
gradlew clean build
for windows and command will clean and build the gradle project and create jar file under/build/libs
D:\spring-boot-gradle-example>gradlew clean build Starting a Gradle Daemon (subsequent builds will be faster) :clean :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 BUILD SUCCESSFUL Total time: 18.599 secs
- Run the Gradle Application : Run the gradle spring boot application using
gradlew bootRun
command.D:\spring-boot-gradle-example>gradlew bootRun :compileJava UP-TO-DATE :processResources UP-TO-DATE :classes UP-TO-DATE :findMainClass :bootRun . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.5.9.RELEASE) 16:46:07.513 INFO 2452 --- [ main] c.t.SpringBootGradleExampleApplication : Starting SpringBootGradleExampleApplication on with PID (D:\spring-boot-gradle-example\build\classes\main started by in D:\spring-boot-gradle-example) 16:46:07.518 INFO 2452 --- [ main] c.t.SpringBootGradleExampleApplication : No active profile set, falling back to default profiles: default 16:46:07.610 INFO 2452 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@438a68: startup [ 16:46:07 IST 2024]; root of context hierarchy 16:46:10.345 INFO 2452 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8082 (http) 16:46:10.370 INFO 2452 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 16:46:10.372 INFO 2452 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.23 16:46:10.556 INFO 2452 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 16:46:10.557 INFO 2452 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2950 ms 16:46:10.808 INFO 2452 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/] 16:46:10.816 INFO 2452 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*] 16:46:10.816 INFO 2452 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*] 16:46:10.817 INFO 2452 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*] 16:46:10.817 INFO 2452 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*] 16:46:11.326 INFO 2452 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@438a68: startup date [ 16:46:07 IST 2024]; root of context hierarchy 16:46:11.441 INFO 2452 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/welcome],methods=[GET]}" onto public java.lang.String com.techgeekenext.TestGradleController.hello() 16:46:11.447 INFO 2452 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest) 16:46:11.447 INFO 2452 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) 16:46:11.491 INFO 2452 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 16:46:11.491 INFO 2452 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 16:46:11.560 INFO 2452 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 16:46:11.819 INFO 2452 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup 16:46:11.922 INFO 2452 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8082 (http) 16:46:11.928 INFO 2452 --- [ main] c.t.SpringBootGradleExampleApplication : Started SpringBootGradleExampleApplication in 5.069 seconds (JVM running for 5.779) <==========---> 80% EXECUTING > :bootRun
OR Run from Intellj IDE as below
Test the Gradle Application
Now, access REST Endpoint at http://localhost:8082/welcome.
Download Source Code
The full source code for this article can be found on below.Download it here - Spring Boot Gradle Example