Spring boot Gradle build project
Gradle created using the IDEA project
Operation is substantially: File-> new-> Project-> Gradle (option in the left column)
After creating a regular-generated project directory as follows:
- build
- gradle
- wrapper
- gradle-wrapper.jar
- gradle-wrapper.properties
- wrapper
- src
- java
- resources
- test
- java
- resources
- build.gradle
- gradlew
- gradlew.bat
- settings.gradle
Spring boot configuration
The following need to edit two files:
Build.gradle contents of the file be modified as follows, is generally dependent on the groupId front, the middle is the artifactId, generally the third version. In repositories configured to use Ali cloud storage, avoid downloading too slow.
plugins {
id 'java'
id 'com.gradle.build-scan' version '2.0.2'
id 'org.springframework.boot' version '2.0.5.RELEASE'
id 'io.spring.dependency-management' version '1.0.7.RELEASE'
}
group 'seckill'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
maven { url "http://maven.aliyun.com/nexus/content/groups/public/" }
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
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' } }
}
}
}
}
}
buildScan {
// always accept the terms of service
termsOfServiceUrl = 'https://gradle.com/terms-of-service'
termsOfServiceAgree = 'yes'
// always publish a build scan
publishAlways()
}
After the contents of the file setting.gradle amended as follows:
rootProject.name = 'seckill'
enableFeaturePreview('IMPROVED_POM_SUPPORT')
Writing class
First, src / java directory generated under the source com.seckill.spring (equivalent com / seckill / spring)
Creating classes in the program entry src / java Application.java, which reads as follows:
package com.seckill.spring;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Created under src / java / com.seckill.spring directory controllers, and controllers directory created class: HelloWorld, which is defined in the root routing and returns Hello World, which code is as follows:
package com.seckill.spring.controllers;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
public class HelloWorld {
@RequestMapping(value = "/", method = RequestMethod.GET)
public Map helloWorld() {
Map<String, Object> ret = new HashMap<>();
ret.put("ret", "Hello World");
return ret;
}
}
Run Access
- Run Application class, you can see from the default listening port in the console in 8080
- Browser Access: localhost: 8080, you can see the return string Hello World