現在開始項目大多數默認使用SpringBoot,下面配置SpringBoot多項目,
之前都是用Maven管理項目依賴,使用https://start.spring.io/ 初始化一個Gradle類型項目作爲參考,配置如下:
主要參考文件build.gradle
plugins {
id 'org.springframework.boot' version '2.2.5.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
}
group = 'org.hazulnut'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}
先配置公共repositories
allprojects {
apply plugin: 'java'
group 'org.hazulnut'
version '1.0-SNAPSHOT'
setProperty('sourceCompatibility', 1.8)
setProperty('targetCompatibility', 1.8)
repositories {
maven { url 'https://maven.aliyun.com/repository/public/' }
maven { url 'https://maven.aliyun.com/repository/central' }
maven { url 'https://maven.aliyun.com/repository/jcenter'}
maven { url 'https://maven.aliyun.com/repository/spring/' }
maven { url 'https://maven.aliyun.com/repository/spring-plugin'}
maven { url 'https://maven.aliyun.com/repository/gradle-plugin'}
mavenLocal()
mavenCentral()
jcenter()
}
}
選一個model項目配置依賴:
plugins {
id 'org.springframework.boot' version '2.2.5.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
build正常,logback項目下依賴可以正常引入:
項目都是依賴於SpringBoot,把公共配置放到root項目中配置,子項目去掉公共配置:
root項目的build.Gradle文件
plugins {
id 'org.springframework.boot' version '2.2.5.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
}
allprojects {
apply plugin: 'java'
group 'org.hazulnut'
version '1.0-SNAPSHOT'
setProperty('sourceCompatibility', 1.8)
setProperty('targetCompatibility', 1.8)
repositories {
maven { url 'https://maven.aliyun.com/repository/public/' }
maven { url 'https://maven.aliyun.com/repository/central' }
maven { url 'https://maven.aliyun.com/repository/jcenter'}
maven { url 'https://maven.aliyun.com/repository/spring/' }
maven { url 'https://maven.aliyun.com/repository/spring-plugin'}
maven { url 'https://maven.aliyun.com/repository/gradle-plugin'}
mavenLocal()
mavenCentral()
jcenter()
}
}
subprojects {
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
}
子項目build.Gradle文件
從上圖紅框標註可以看到所有子項目都正常引入公共依賴。
注意:root項目的build.Gradle中的plugins配置塊不能缺少,否則報下圖所示錯誤。
至此 Gradle5.2.1+ 構建 SpringBoot多項目完成。
Github項目 https://github.com/HazelNutsWorkGroup/nuts.springboot.single ,
Gitee項目 https://gitee.com/sleeber/nuts.springboot.single
歡迎大家交流