Create a simple spring boot application using gradle

Juan Enciso Condeña

--

Sometimes you need to create a simple application to print a classic “Hello World!” in order to test a basic procedure, a pipeline or check if your java environment is OK.

Gradle website has great tutorial explains step by step how to create your first java application, these are the steps:

Initialize your project

gradle init --type java-application

Then, you will have this files:

$ tree spring-hello-world/
spring-hello-world/
├── build.gradle
├── gradle
│ └── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
└── src
├── main
│ ├── java
│ │ └── spring
│ │ └── hello
│ │ └── world
│ │ └── App.java
│ └── resources
└── test
├── java
│ └── spring
│ └── hello
│ └── world
│ └── AppTest.java
└── resources

15 directories, 8 files

Customize the `build.gradle` file

plugins {
// Apply the java plugin to add support for Java
id 'java'

// Apply the application plugin to add support for building a CLI application.
id 'application'

// Spring Boot plugins
id 'org.springframework.boot' version '2.0.5.RELEASE'
id 'io.spring.dependency-management' version '1.0.7.RELEASE'
}

Defining the dependencies

dependencies {
implementation 'org.springframework.boot:spring-boot-dependencies:2.0.5.RELEASE'
implementation 'org.springframework.boot:spring-boot-starter-web'

testImplementation 'org.springframework.boot:spring-boot-starter-test'

components {
withModule('org.springframework:spring-beans') {
allVariants {
withDependencyConstraints {
// Need to patch constraints because snakeyaml is an optional dependency
it.findAll { it.name == 'snakeyaml' }.each { it.version { strictly '1.19' } }
}
}
}
}
}

Creating a “Hello World” sample application

mkdir src/{main,test}/java/hello

Inside the src/main/java/hello directory, create these two files:

App.java

package hello;

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

@SpringBootApplication
public class App {

public static void main(String[] args) {
SpringApplication.run(App.class, args);
}

}

HelloGradleController.java

package hello;

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

@RestController("/")
public class HelloGradleController {

@GetMapping
public String helloGradle() {
return "Hello Gradle!";
}

}

And create this file: src/test/java/hello/AppTest.java

package hello;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = App.class)
@AutoConfigureMockMvc
public class AppTest {

@Autowired
private MockMvc mvc;

@Test
public void helloGradle() throws Exception {
mvc.perform(get("/"))
.andExpect(status().isOk())
.andExpect(content().string("Hello Gradle!"));
}

}

Define the name of the main class for the spring boot jar file. Add this content in the build.gradle file

application {
mainClassName = 'hello.App'
}

bootJar {
mainClassName = 'hello.App'
}

At this point, you could build and run the spring boot application

./gradlew bootJar

Or using the jar file

java -jar ./build/libs/spring-hello-world.jar

Another way to run the Application is by executing the following Gradle command:

./gradlew bootRun

--

--

No responses yet

Write a response