Gradle 的多项目构建功能允许开发者在一个构建中管理多个相关的项目,这对于大型应用程序和微服务架构非常有用。以下是 Gradle 多项目构建的详细说明:
多项目构建结构
基本目录结构
shellmy-project/ ├── settings.gradle ├── build.gradle ├── app/ │ ├── build.gradle │ └── src/ ├── library/ │ ├── build.gradle │ └── src/ └── common/ ├── build.gradle └── src/
settings.gradle 配置
groovy// settings.gradle rootProject.name = 'my-project' // 包含子项目 include 'app', 'library', 'common' // 使用相对路径包含项目 include ':data:repository' project(':data:repository').projectDir = new File(rootDir, 'modules/data/repository') // 排除项目 // include 'excluded-module'
项目配置
根项目配置
groovy// build.gradle (根项目) allprojects { group = 'com.example' version = '1.0.0' repositories { mavenCentral() } } subprojects { apply plugin: 'java' java { sourceCompatibility = JavaVersion.VERSION_17 } dependencies { implementation 'org.slf4j:slf4j-api:2.0.7' } }
特定项目配置
groovy// app/build.gradle dependencies { implementation project(':library') implementation project(':common') testImplementation project(':common').sourceSets.test.output } // library/build.gradle dependencies { api project(':common') }
项目依赖
项目间依赖
groovy// 使用 project() 方法 dependencies { implementation project(':library') testImplementation project(':common').sourceSets.test.output // 使用配置 implementation project(path: ':library', configuration: 'runtimeClasspath') }
依赖配置
groovy// 在被依赖的项目中定义配置 // library/build.gradle configurations { apiElements { canBeResolved = false canBeConsumed = true attributes { attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage, 'java-api')) } } } // 在依赖项目中使用 // app/build.gradle dependencies { implementation project(path: ':library', configuration: 'apiElements') }
项目属性和配置
项目属性
groovy// 定义项目属性 ext { springBootVersion = '3.0.0' junitVersion = '5.9.0' } // 在子项目中访问 // app/build.gradle dependencies { implementation "org.springframework.boot:spring-boot-starter-web:${springBootVersion}" }
条件配置
groovy// 根据项目名称条件配置 configure(subprojects.findAll { it.name.startsWith('web-') }) { apply plugin: 'war' } // 根据项目属性条件配置 subprojects { if (project.hasProperty('enableJacoco')) { apply plugin: 'jacoco' } }
共享配置
使用配置注入
groovy// build.gradle (根项目) subprojects { // 配置所有子项目 apply plugin: 'java' // 配置 Java 编译 tasks.withType(JavaCompile).configureEach { options.encoding = 'UTF-8' options.compilerArgs << '-Xlint:unchecked' } // 配置测试 test { useJUnitPlatform() testLogging { events 'passed', 'skipped', 'failed' } } }
使用约定插件
groovy// buildSrc/src/main/groovy/JavaLibraryPlugin.groovy class JavaLibraryPlugin implements Plugin<Project> { void apply(Project project) { project.with { apply plugin: 'java-library' java { sourceCompatibility = JavaVersion.VERSION_17 targetCompatibility = JavaVersion.VERSION_17 } dependencies { api platform('org.springframework.boot:spring-boot-dependencies:3.0.0') } } } } // 在子项目中使用 // library/build.gradle plugins { id 'java-library-convention' }
构建任务
在所有项目中运行任务
bash# 在所有项目中运行 clean 任务 ./gradlew clean # 在所有项目中运行 test 任务 ./gradlew test # 运行特定项目的任务 ./gradlew :app:build ./gradlew :library:test
任务依赖
groovy// app/build.gradle tasks.named('build') { dependsOn ':library:build', ':common:build' } // 根项目 build.gradle tasks.register('buildAll') { dependsOn subprojects.collect { "${it.path}:build" } }
多项目构建最佳实践
1. 合理的项目划分
groovy// 按功能模块划分 include 'core', 'api', 'web', 'data', 'service' // 按层次划分 include 'common', 'infrastructure', 'domain', 'application'
2. 共享依赖管理
groovy// build.gradle (根项目) ext { versions = [ springBoot: '3.0.0', junit: '5.9.0', mockito: '5.0.0' ] libs = [ springBootWeb: "org.springframework.boot:spring-boot-starter-web:${versions.springBoot}", junit: "org.junit.jupiter:junit-jupiter:${versions.junit}", mockito: "org.mockito:mockito-core:${versions.mockito}" ] } // 在子项目中使用 // app/build.gradle dependencies { implementation libs.springBootWeb testImplementation libs.junit testImplementation libs.mockito }
3. 使用版本目录
groovy// gradle/libs.versions.toml [versions] spring-boot = "3.0.0" junit = "5.9.0" [libraries] spring-boot-web = { module = "org.springframework.boot:spring-boot-starter-web", version.ref = "spring-boot" } jupiter = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit" } [plugins] java = { id = "java" } spring-boot = { id = "org.springframework.boot", version = "3.0.0" } // 在子项目中使用 // app/build.gradle plugins { id libs.plugins.java.get().pluginId } dependencies { implementation libs.spring.boot.web testImplementation libs.jupiter }
4. 避免循环依赖
groovy// 检查循环依赖 ./gradlew :app:dependencies --configuration runtimeClasspath // 使用依赖分析工具 plugins { id 'com.github.dependency-license-report' version '2.5' }
性能优化
并行构建
groovy// gradle.properties org.gradle.parallel=true org.gradle.caching=true org.gradle.configureondemand=true
配置优化
groovy// 使用延迟配置 subprojects { tasks.register('customTask') { // 任务只在需要时创建 } } // 避免在配置阶段执行耗时操作 subprojects { // 不要在这里进行网络请求或文件 I/O }
常用命令
bash# 查看项目结构 ./gradlew projects # 查看所有任务 ./gradlew tasks # 查看特定项目的任务 ./gradlew :app:tasks # 查看项目依赖 ./gradlew :app:dependencies # 构建所有项目 ./gradlew build # 构建特定项目 ./gradlew :app:build # 清理所有项目 ./gradlew clean # 并行构建 ./gradlew build --parallel