Nowadays, you can create a Spring Boot project with just one click such as eclipse. But how do you create a Spring Boot project without the help of eclipse? I would like to write up to the point where RestController returns HelloWorld based on memorandum. (It's not that I don't use eclipse.)
I have installed and running the following on my Mac.
First, create an empty directory and create a Gradle project.
#Create an empty directory
mkdir GradleSpringBoot
#Gradle project
cd GradleSpringBoot
gradle init
#After typing the init command, the following options will appear, so select the one that applies.
#This time, we will select it to be a Java project.
Select type of project to generate:
1: basic
2: application
3: library
4: Gradle plugin
Enter selection (default: basic) [1..4] 2
Select implementation language:
1: C++
2: Groovy
3: Java
4: Kotlin
Enter selection (default: Java) [1..4] 3
Select build script DSL:
1: Groovy
2: Kotlin
Enter selection (default: Groovy) [1..2] 1
Select test framework:
1: JUnit 4
2: TestNG
3: Spock
4: JUnit Jupiter #It's JUnit 5. If you don't have a reason, choose JUnit 5 as it's new.
Enter selection (default: JUnit 4) [1..4] 4
Rewrite build.gradle so that you can import your Gradle project into eclipse.
id 'application'
//Let's add the following plugin around the 15th line.
id 'eclipse'
After editing and saving, let's execute the following command.
#"Necessary to recognize as an eclipse project".project」「.Create a "setting".
gradle eclipse
At this point, you can import it into eclipse, so Let's import with import-> existing projects into workspace.
The dependency of SpringBoot is summarized by Gradle official site, so please refer to this for build. Edit .grale.
build.gradle
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java project to get you started.
* For more details take a look at the Java Quickstart chapter in the Gradle
* User Manual available at https://docs.gradle.org/5.5.1/userguide/tutorial_java_projects.html
*/
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'
id 'eclipse'
id 'com.gradle.build-scan' version '2.3'
id 'org.springframework.boot' version '2.1.6.RELEASE'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
}
repositories {
// Use jcenter for resolving dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}
dependencies {
// This dependency is used by the application.
implementation 'com.google.guava:guava:27.1-jre'
implementation 'org.springframework.boot:spring-boot-dependencies:2.1.6.RELEASE'
implementation 'org.springframework.boot:spring-boot-starter-web:2.1.6.RELEASE'
// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test
testImplementation 'org.springframework.boot:spring-boot-starter-test:2.1.6.RELEASE'
// Use JUnit Jupiter API for testing.
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.4.2'
// Use JUnit Jupiter Engine for testing.
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.4.2'
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' } }
}
}
}
}
}
bootJar {
mainClassName = 'co.jp.study.App'
}
buildScan {
// always accept the terms of service
termsOfServiceUrl = 'https://gradle.com/terms-of-service'
termsOfServiceAgree = 'yes'
// always publish a build scan
publishAlways()
}
test {
// Use junit platform for unit tests
useJUnitPlatform()
}
The version of each library is set to the latest one (as of July 26, 2019) while checking with maven.
We will create a POJO class to start SpringBoot and a RestController to return HelloWorld.
App.java
package co.jp.study;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
*SpringBoot boot class
*/
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
HelloController
package co.jp.study.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
*Sample RestController class
*/
@RestController("/")
public class HelloContoller {
@RequestMapping
public String hello() {
return "Hello World!";
}
}
Execute the following command on the console Create siteiki and Jar and start Spring Boot.
#Creating a jar
gradle bootJar
#Run
gradle bootRun
If you access the following address and output "Hello World", it is successful. http://localhost:8080/
Thank you for your hard work. Thank you for reading the long text. Let's send a comfortable Spring Boot development.
Recommended Posts