最墨迹的方法
上文介绍了一个使用官网的初始项目来创建新项目的方法,后来经过多次实践以后发现其实还是不够好,不够快。或者可以说是最墨迹的方法。于是笔者开始探索更快、更方便的方法。多方比较之后,发现只有一个方法是最快的。那就是:Gradle。
用Gradle来创建SpringBoot项目
其实,最快最好也是要看使用的方法和工具是不是最熟悉的。因为一直开发Android的关系,使用Gradle多一些,所以使用起来比较顺手。后面就用Intellij IDEA的Community版来演示如何快速的搭建项目。
具体步骤:
1.用创建一个Gradle项目。这样项目会自动生成及格简单的文件。其中需要我们关注的就是build.gradle
这个文件。之后的配置都是在这个文件中的。
Gradle项目就是这么简单,其他的重点就在与Gradle中添加plugin和依赖项了。
刚刚生成的这个项目的build.gradle
文件中只有两行:
group 'com.spring.test'
version '1.0-SNAPSHOT'
这两行是你在创建项目的时候填写的内容的一部分:1. 你填写的groupId,2. 是你填写的版本。这里的版本内容是默认的。
2.需要创建项目的目录。否则代码的组织会出现问题。而且会有一些配置文件无法被识别。
└── src
└── main
└── java
└── app
3.开始详细的配置Gradle文件。
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.3.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
maven{
url "http://jcenter.bintray.com"
}
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
// tag::jetty[]
compile("org.springframework.boot:spring-boot-starter-web") {
exclude module: "spring-boot-starter-tomcat"
}
compile("org.springframework.boot:spring-boot-starter-jetty")
// end::jetty[]
// tag::actuator[]
compile("org.springframework.boot:spring-boot-starter-actuator")
// end::actuator[]
compile("org.springframework.boot:spring-starter-data-jpa")
compile("mysql:mysql-connector-java:5.1.38")
}
把以上部分直接复制粘贴过去就可以了。前文提到的创建项目默认生成的两行放在最前不要覆盖!
在IDE的最右寻找Gradle,点击这个按钮之后就会弹出一个小窗口。点一下刷新小按钮之后就都搞定了。剩下的就交给IDE去完成吧。
4.这一步是可选的。如果你还想用Kotlin开发的话就需要用下面的配置文件代替上面的。
buildscript {
ext.kotlin_version = '1.0.1-1'
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.3.RELEASE")
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: "kotlin"
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
maven{
url "http://jcenter.bintray.com"
}
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
// tag::jetty[]
compile("org.springframework.boot:spring-boot-starter-web") {
exclude module: "spring-boot-starter-tomcat"
}
compile("org.springframework.boot:spring-boot-starter-jetty")
// end::jetty[]
// tag::actuator[]
compile("org.springframework.boot:spring-boot-starter-actuator")
// end::actuator[]
compile("org.springframework.boot:spring-starter-data-jpa")
compile("mysql:mysql-connector-java:5.1.38")
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
5.到这一步已经没有太多需要说明的了。只要添加必须的Application
其实叫什么都可以,只要包含一个main()
方法就好。添加需要的Controller
等。
其他可以参考上篇文章的内容。