Gradle 如何实现多项目构建?如何配置项目间的依赖关系?
Gradle 的多项目构建功能允许开发者在一个构建中管理多个相关的项目,这对于大型应用程序和微服务架构非常有用。以下是 Gradle 多项目构建的详细说明:多项目构建结构基本目录结构my-project/├── settings.gradle├── build.gradle├── app/│ ├── build.gradle│ └── src/├── library/│ ├── build.gradle│ └── src/└── common/ ├── build.gradle └── src/settings.gradle 配置// settings.gradlerootProject.name = 'my-project'// 包含子项目include 'app', 'library', 'common'// 使用相对路径包含项目include ':data:repository'project(':data:repository').projectDir = new File(rootDir, 'modules/data/repository')// 排除项目// include 'excluded-module'项目配置根项目配置// 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' }}特定项目配置// app/build.gradledependencies { implementation project(':library') implementation project(':common') testImplementation project(':common').sourceSets.test.output}// library/build.gradledependencies { api project(':common')}项目依赖项目间依赖// 使用 project() 方法dependencies { implementation project(':library') testImplementation project(':common').sourceSets.test.output // 使用配置 implementation project(path: ':library', configuration: 'runtimeClasspath')}依赖配置// 在被依赖的项目中定义配置// library/build.gradleconfigurations { apiElements { canBeResolved = false canBeConsumed = true attributes { attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage, 'java-api')) } }}// 在依赖项目中使用// app/build.gradledependencies { implementation project(path: ':library', configuration: 'apiElements')}项目属性和配置项目属性// 定义项目属性ext { springBootVersion = '3.0.0' junitVersion = '5.9.0'}// 在子项目中访问// app/build.gradledependencies { implementation "org.springframework.boot:spring-boot-starter-web:${springBootVersion}"}条件配置// 根据项目名称条件配置configure(subprojects.findAll { it.name.startsWith('web-') }) { apply plugin: 'war'}// 根据项目属性条件配置subprojects { if (project.hasProperty('enableJacoco')) { apply plugin: 'jacoco' }}共享配置使用配置注入// 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' } }}使用约定插件// buildSrc/src/main/groovy/JavaLibraryPlugin.groovyclass 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.gradleplugins { id 'java-library-convention'}构建任务在所有项目中运行任务# 在所有项目中运行 clean 任务./gradlew clean# 在所有项目中运行 test 任务./gradlew test# 运行特定项目的任务./gradlew :app:build./gradlew :library:test任务依赖// app/build.gradletasks.named('build') { dependsOn ':library:build', ':common:build'}// 根项目 build.gradletasks.register('buildAll') { dependsOn subprojects.collect { "${it.path}:build" }}多项目构建最佳实践1. 合理的项目划分// 按功能模块划分include 'core', 'api', 'web', 'data', 'service'// 按层次划分include 'common', 'infrastructure', 'domain', 'application'2. 共享依赖管理// 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.gradledependencies { implementation libs.springBootWeb testImplementation libs.junit testImplementation libs.mockito}3. 使用版本目录// 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.gradleplugins { id libs.plugins.java.get().pluginId}dependencies { implementation libs.spring.boot.web testImplementation libs.jupiter}4. 避免循环依赖// 检查循环依赖./gradlew :app:dependencies --configuration runtimeClasspath// 使用依赖分析工具plugins { id 'com.github.dependency-license-report' version '2.5'}性能优化并行构建// gradle.propertiesorg.gradle.parallel=trueorg.gradle.caching=trueorg.gradle.configureondemand=true配置优化// 使用延迟配置subprojects { tasks.register('customTask') { // 任务只在需要时创建 }}// 避免在配置阶段执行耗时操作subprojects { // 不要在这里进行网络请求或文件 I/O}常用命令# 查看项目结构./gradlew projects# 查看所有任务./gradlew tasks# 查看特定项目的任务./gradlew :app:tasks# 查看项目依赖./gradlew :app:dependencies# 构建所有项目./gradlew build# 构建特定项目./gradlew :app:build# 清理所有项目./gradlew clean# 并行构建./gradlew build --parallel