Gangmax Blog

Create Spring Boot Gradle Project

The purpose is to create a Spring Boot project in Gradle flavor. Refer here.

  • Use Spring Initializr to generate the basic project structure. Use “Gradle Project/Java/2.0.0” options, and fill the “GroupId/ArtifactId” fields with the values you want. In this case they are “com.example/demo”. Then click the “Generate Project” button to download the zip file.

  • Unzip the zip file to a directory. It’s already a complete Gradle project with Spring Boot.

  • Update the “build.gradle” file, such as adding “idea” plugin, dependency version numbers and etc. The final content is:

build.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
buildscript {
ext {
springBootVersion = '2.0.0.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
mavenCentral()
}

dependencies {
compile("org.springframework.boot:spring-boot-starter-web:2.0.0.RELEASE")
runtime('org.springframework.boot:spring-boot-devtools:2.0.0.RELEASE')
testCompile('org.springframework.boot:spring-boot-starter-test:2.0.0.RELEASE')
}

bootJar {
baseName = 'demo-spring-boot'
version = '0.1.0'
}
  • Now you can run the following command to generate the IntelliJ IDEA project artifacts(demo.iml/demo.ipr/demo.iws).
1
2
3
4
5
6
# If you have Gradle installed in your environment:
gradle build
# If you don't have Gradle installed in your Unix like OS(Linux/MacOS):
./gradlew build
# If you don't have Gradle installed in your Windows:
.\gradlew.bat build

After the command executes successfully, open the “demo.ipr” file in IntelliJ IDEA.

  • Create a “controller” class like below:
HelloController
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.example.demo;

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

@RestController
public class HelloController {

@RequestMapping("/")
public String index() {
return "Greetings from Spring Boot!";
}

}
  • Update the existing “DemoApplication” class to:
DemoApplication.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.example.demo;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

import java.util.Arrays;

/**
* Then entrance of the whole application. By default the application is a stand-alone Java application. If you
* want to create a deployable war file, please following the instructions
* [here](https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-create-a-deployable-war-file).
*/
@SpringBootApplication
public class DemoApplication {

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

@Bean
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
return args -> {

System.out.println("Let's inspect the beans provided by Spring Boot:");
System.out.println("Args length:" + args.length);
String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
System.out.println(beanName);
}

};
}

}
  • No the demo project is ready. Run the following commands to run the application:
1
java -jar build/libs/demo-spring-boot-0.1.0.jar``

After the application starts, you can access “http://localhost:8080/“ to view the result.

Comments