Gradle
因爲構建速度比Maven
快,且比Maven
靈活,所以不少後端的應用都使用了Gradle
進行構建,但一個問題是,Gradle
的多模塊項目比較難構建,再加上Gradle
的更新很是快,這就給構建一個多模塊Gradle
項目形成了很多的困難。java
基於此出發點,本文提供了兩種形式的使用Gradle
構建的Spring Boot
多模塊項目:git
Java + Gradle
Kotlin + Gradle + Kotlin DSL
爲了減小出現各類錯誤的機率,步驟作得很是詳細(多圖預警),文末也附上了源碼,下面就一塊兒來看看吧。github
Gradle 6.8.2
Spring Boot 2.4.3
Kotlin 1.4.30
Open JDK 11
Java + Gradle
主要步驟:web
Spring Initializer
建立項目build.gradle
直接使用IDEA
提供的Spring Initializer
便可,構建工具選擇Gradle
:spring
依賴:後端
構建完成後刪除src
目錄,由於根目錄屬於管理模塊目錄不提供運行的應用:api
build.gradle
這是最複雜的一步,而且Gradle
版本更新的話步驟可能會不同,首先在底部添加一個空的subprojects
:瀏覽器
接着把dependencies
以及test
移動進去:bash
最後一步是,在subprojects
開頭,添加插件apply
,根據默認初始化建立的plugins
,逐一添加。app
好比這裏默認使用了三個插件:
apply
到subprojects
中:
File -> New -> Module
:
輸入模塊名便可,這裏的例子是建立兩個模塊:
service
app
建立好後如圖所示:
完成建立以後,把兩個模塊中的build.gradle
除了repositories
以外的所有刪去,僅保留repositories
:
service
模塊首先建立包,根據根目錄中的group
建立:
接着編寫一個叫TestService
的帶@Service
註解的類,裏面包含一個test
方法:
同時修改service
模塊的build.gradle
,添加bootJar
以及jar
選項:
bootJar{ enabled = false } jar{ enabled = true }
app
模塊一樣先根據根目錄的group
建立包:
接着在app
模塊的build.gradle
添加service
模塊的依賴:
再建立啓動類以及一個Controller
:
代碼以下:
package com.example; 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); } }
package com.example.controller; import com.example.service.TestService; import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequiredArgsConstructor(onConstructor = @__(@Autowired)) public class TestController { private final TestService service; @GetMapping("/test") public String test(){ return service.test(); } }
接下來就能夠運行了,能夠直接點擊Application
旁邊的綠色小三角:
或者從運行配置中選擇Application
運行(IDEA
自動建立的,原來的那個DemoApplication
帶一個×是由於啓動文件已經刪除了,能夠順便把該配置刪除):
沒問題的話就能夠成功運行了:
同時瀏覽器訪問localhost:8080/test
會出現test
字樣:
在建立測試類以前,也須要先建立包,且須要確保包名與啓動類的包名一致:
再建立測試類:
package com.example; import com.example.service.TestService; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest public class JavaTest { @Autowired private TestService service; @Test public void test(){ System.out.println(service.test()); } }
接着進行測試:
這樣使用Java
+Gradle
構建一個多模塊的Spring Boot
項目就成功了。
Kotlin + Gradle + Kotlin DSL
Kotlin DSL
在原生Gradle
(Groovy DSL
)的基礎上進行改進,但同時語法也變得更加陌生,難度所以也加大了很多,不過這並無難倒筆者。構建多模塊的基本步驟與上面相似:
Spring Initializer
建立項目build.gradle.kts
選擇Kotlin
+Gradle
:
依賴:
一樣刪除src
:
build.gradle.kts
一樣在尾部添加一個空的subprojects
:
把dependencies
以及tasks
移動進去:
最後在subprojects
開始處apply
插件,根據默認的插件進行apply
:
代碼以下:
apply{ plugin("io.spring.dependency-management") plugin("org.springframework.boot") plugin("org.jetbrains.kotlin.plugin.spring") plugin("org.jetbrains.kotlin.jvm") }
plugins
中的kotlin
是org.jetbrains.kotlin
的簡寫,在subprjects
中注意加上便可。
File -> New -> Module
,把一些必要選項勾選上:
這裏一樣建立兩個模塊:
app
service
一樣把兩個模塊中的build.gradle.kts
刪除其餘部分留下repositories
:
service
模塊首先根據根目錄的build.gradle.kts
建立包:
編寫TestService
:
最後修改build.gradle.kts
,加上tasks.bootJar
與tasks.jar
:
tasks.bootJar{ enabled = false } tasks.jar{ enabled = true }
app
模塊先建立包:
添加對service
模塊的依賴:
再建立一個啓動類以及一個Controller
:
代碼以下:
package com.example import org.springframework.boot.SpringApplication import org.springframework.boot.autoconfigure.SpringBootApplication @SpringBootApplication class Application fun main(args:Array<String>) { SpringApplication.run(Application::class.java,*args) }
package com.example.controller import com.example.service.TestService import org.springframework.beans.factory.annotation.Autowired import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.RestController @RestController class TestController { @Autowired lateinit var service: TestService @GetMapping("/test") fun test() = service.test() }
點擊main
旁邊的綠色小三角便可:
運行成功:
一樣能夠訪問localhost:8080/test
:
注意在編寫測試以前須要保證測試類與啓動類在同一個包下,也就是須要先建立包:
再建立測試類:
package com.example import com.example.service.TestService import org.junit.jupiter.api.Test import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.test.context.SpringBootTest @SpringBootTest class KotlinTest { @Autowired lateinit var service: TestService @Test fun test(){ println(service.test()) } }
直接點擊小三角測試便可:
測試經過,這樣Kotlin+Gradle+Kotlin DSL
的多模塊Spring Boot
項目就算建立完成了。
筆者在實踐的過程當中也遇到了無數的錯誤,好比找不到類,或者build.gradle
/build.gradle.kts
文件錯誤,幸虧有萬能的搜索引擎,幫筆者解決了錯誤,最後才成功寫下這篇文章。
總的來講,Gradle
建立多模塊項目要比Maven
要難,並且Gradle
的更新速度很快,語法變化較大,相比之下Maven
很是穩定,最新的Maven 3.6.3
仍是19年11月發佈的,然而Gradle
都準備7.0
了:
筆者建議,若是是真的須要使用Gradle
,須要考慮一下團隊的情況,畢竟上手難度要大於Maven
,若是在Gradle
建立多模塊的過程當中遇到一些極其難以解決的問題,轉爲Maven
不失爲一個好辦法。
附上兩個例子的源碼: