首先从官方下载一个SpringBoot项目
git clone https://github.com/spring-guides/gs-multi-module.git
修改 gradle/wrapper/gradle-wrapper.properties 文件当中的gradle版本,结果如下
#Sun Dec 19 10:45:20 CST 2021
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.1-all.zip
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
修改模块中的 complete/library/build.gradle 的仓库配置
plugins {
id 'org.springframework.boot' version '2.5.2' apply false
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenLocal()
maven { url 'https://maven.aliyun.com/repository/public/' }
mavenCentral()
}
dependencyManagement {
imports {
mavenBom org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES
}
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
修改模块中的 complete/application/build.gradle 的仓库配置
plugins {
id 'org.springframework.boot' version '2.5.2'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenLocal()
maven { url 'https://maven.aliyun.com/repository/public/' }
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation project(':library')
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
将项目导入到IDEA当中,结构如下图所示
不难看出,这里分为两个模块 library 和 application,在根路径当中的 settings.gradle 配置脚本中通过 include 关键字包含。每个子模块当中的 settings.gradle 配置脚本则只需要定义项目名称,比如
rootProject.name = 'library'
rootProject.name = 'application'
这里的两个模块并不是相互独立的,而是存在依赖关系,application会依赖于library,和maven一样,是通过将被依赖模块作为一个依赖配置进来的。在application模块中的build.gradle包含如下定义达到引用其他模块的目的
implementation project(':library')
在这里还可以看到一种依赖版本管理的定义方式
dependencyManagement {
imports {
mavenBom org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES
}
}
本质就是使用的maven的dependencyManagement而已。
对于多模块项目来说,gradle操作既可以作用于子模块,也可以作用于聚合模块,如下图所示
这里的聚合模块是gs-multi-module,执行这个模块的build任务会编译所有的子模块,而自己并没有实际的任务,结果如下
11:09:41: Executing task 'build'...
> Task :library:compileJava
> Task :library:processResources NO-SOURCE
> Task :library:classes
> Task :library:jar
> Task :application:compileJava
> Task :application:processResources
> Task :application:classes
> Task :application:bootJarMainClassName
> Task :application:bootJar
> Task :application:jar
> Task :application:assemble
> Task :application:compileTestJava
> Task :application:processTestResources NO-SOURCE
> Task :application:testClasses
> Task :application:test
> Task :application:check
> Task :application:build
> Task :library:assemble
> Task :library:compileTestJava
> Task :library:processTestResources NO-SOURCE
> Task :library:testClasses
> Task :library:test
> Task :library:check
> Task :library:build
BUILD SUCCESSFUL in 23s
11 actionable tasks: 11 executed
这里仅仅是执行了application子模块和library子模块的任务。所谓最终只有子模块有构建的结果