本人使用的idea是社区版:ideaIC-2018.1.5
1.create new project
2.选择gradle+java+jdk1.8
3.填写groupId和artifactId
4.选择本地gradle
5.然后finish,等待gradle build完成,此时会发现没有src目录,可以手动建,也可以使用gradle task命令
6.使用task建src目录,将以下代码复制放在build.gradle中,然后右下角会出现一个提示,点击import changes,会发现右侧gradle-tasks-other下出现了create,然后双击,此时发现左侧项目下已经有src目录(文末有补充)
task "create" << {
sourceSets*.java.srcDirs*.each{it.mkdirs()}
sourceSets.main.resources.srcDirs.each{it.mkdirs()}
}
7.当然是删除task,然后修改build.gradle添加springboot
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.2.RELEASE")
}
}
repositories {
mavenCentral()
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
testCompile("org.springframework.boot:spring-boot-starter-test")
}
然后刷新build
8.增加springboot启动类和测试类
Application.java
package org.cth.test;
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);
}
}
HelloController.java
package org.cth.test.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("test")
public class HelloController {
private Logger log = LoggerFactory.getLogger(HelloController.class);
@GetMapping("hello")
public String hello(){
log.info(">>>>>>>>1111111111111111");
return "hello Mr chen";
}
}
9.启动项目(springboot自带tomcat),点击gradle的run,端口号为8080,url(不需要加工程名)为http://localhost:8080/test/hello
补充:关于第六点 使用task建src目录,改进了一下,可以初始化一些想要的目录及文件,比如我想增加kotlin目录
task create{
sourceSets*.java.srcDirs*.each{it.mkdirs()}
sourceSets.main.resources.srcDirs.each{it.mkdirs()}
doLast {
def dir = new File('src/main/kotlin')
if (!dir.exists()) {
dir.mkdirs()
}
def file = file 'src/main/kotlin/Application.kt'
if (!file.exists()) {
file.createNewFile()
}
}
}