最墨跡的方法
上文介紹了一個使用官網的初始項目來創建新項目的方法,后來經過多次實踐以后發現其實還是不夠好,不夠快。或者可以說是最墨跡的方法。于是筆者開始探索更快、更方便的方法。多方比較之后,發現只有一個方法是最快的。那就是: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
等。
其他可以參考上篇文章的內容。