乐闻世界logo
搜索文章和话题

Gradle 如何管理依赖?有哪些依赖配置类型?

2月21日 18:10

Gradle 的依赖管理系统是其核心功能之一,提供了强大而灵活的依赖管理能力。以下是 Gradle 依赖管理的详细说明:

依赖声明

基本语法

groovy
dependencies { // 格式: group:name:version implementation 'org.springframework.boot:spring-boot-starter-web:3.0.0' // 使用 map 语法 implementation group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '3.0.0' }

依赖配置类型

编译时依赖

groovy
dependencies { // implementation:编译和运行时需要,但不暴露给依赖此项目的消费者 implementation 'org.apache.commons:commons-lang3:3.12.0' // compileOnly:编译时需要,但运行时不需要(如 Lombok) compileOnly 'org.projectlombok:lombok:1.18.24' // annotationProcessor:注解处理器 annotationProcessor 'org.projectlombok:lombok:1.18.24' }

运行时依赖

groovy
dependencies { // runtimeOnly:运行时需要,但编译时不需要 runtimeOnly 'mysql:mysql-connector-java:8.0.28' }

测试依赖

groovy
dependencies { // testImplementation:测试编译和运行时需要 testImplementation 'org.junit.jupiter:junit-jupiter:5.9.0' // testCompileOnly:测试编译时需要 testCompileOnly 'org.projectlombok:lombok:1.18.24' // testRuntimeOnly:测试运行时需要 testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.0' }

传递依赖

groovy
dependencies { // api:编译和运行时需要,且暴露给消费者 api 'org.apache.commons:commons-math3:3.6.1' // compileOnlyApi:编译时需要,运行时不需要,但暴露给消费者 compileOnlyApi 'org.apache.commons:commons-text:1.10.0' }

仓库配置

常用仓库

groovy
repositories { // Maven 中央仓库 mavenCentral() // Google 仓库(Android 开发常用) google() // Gradle 插件仓库 gradlePluginPortal() // 自定义 Maven 仓库 maven { url 'https://maven.aliyun.com/repository/public' name 'Aliyun Public' } // 本地 Maven 仓库 mavenLocal() // Ivy 仓库 ivy { url 'https://example.com/ivy-repo' layout 'pattern' } }

仓库认证

groovy
repositories { maven { url 'https://example.com/private-repo' credentials { username = 'admin' password = 'password' } } }

依赖版本管理

动态版本

groovy
dependencies { // 最新版本(不推荐生产环境使用) implementation 'org.springframework.boot:spring-boot-starter-web:+' // 最新发布版本 implementation 'org.springframework.boot:spring-boot-starter-web:latest.release' // 最新集成版本 implementation 'org.springframework.boot:spring-boot-starter-web:latest.integration' // 版本范围 implementation 'org.apache.commons:commons-lang3:[3.10,4.0)' implementation 'org.apache.commons:commons-lang3:3.+' }

使用版本目录(推荐)

groovy
// 在 gradle/libs.versions.toml 中定义 [versions] spring-boot = "3.0.0" commons-lang3 = "3.12.0" [libraries] spring-boot-web = { module = "org.springframework.boot:spring-boot-starter-web", version.ref = "spring-boot" } commons-lang3 = { module = "org.apache.commons:commons-lang3", version.ref = "commons-lang3" } // 在 build.gradle 中使用 dependencies { implementation libs.spring.boot.web implementation libs.commons.lang3 }

使用 ext 属性

groovy
ext { springBootVersion = '3.0.0' commonsLang3Version = '3.12.0' } dependencies { implementation "org.springframework.boot:spring-boot-starter-web:${springBootVersion}" implementation "org.apache.commons:commons-lang3:${commonsLang3Version}" }

依赖排除

排除特定依赖

groovy
dependencies { implementation('org.springframework.boot:spring-boot-starter-web:3.0.0') { exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat' } }

全局排除

groovy
configurations { all { exclude group: 'org.slf4j', module: 'slf4j-log4j12' } }

依赖解析策略

强制指定版本

groovy
configurations.all { resolutionStrategy { force 'org.apache.commons:commons-lang3:3.12.0' } }

冲突解决

groovy
configurations.all { resolutionStrategy { // 失败策略 failOnVersionConflict() // 使用最新版本 failOnVersionConflict() // 使用特定版本 force 'org.apache.commons:commons-lang3:3.12.0' } }

传递依赖管理

查看依赖树

bash
# 查看所有依赖 ./gradlew dependencies # 查看特定配置的依赖 ./gradlew dependencies --configuration implementation # 查看特定任务的依赖 ./gradlew :app:dependencies

停止传递依赖

groovy
dependencies { implementation('com.example:library:1.0.0') { transitive = false } }

BOM(Bill of Materials)管理

导入 BOM

groovy
dependencies { // 导入 Spring Boot BOM implementation platform('org.springframework.boot:spring-boot-dependencies:3.0.0') // 导入后无需指定版本 implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-data-jpa' }

自定义 BOM

groovy
// 创建 platform 模块 dependencies { api platform('com.example:bom:1.0.0') }

最佳实践

  1. 使用版本目录:统一管理依赖版本,避免版本冲突
  2. 避免使用动态版本:在生产环境中使用固定版本
  3. 合理选择依赖配置:根据实际需求选择 implementation、api 等
  4. 定期更新依赖:使用 ./gradlew dependencyUpdates 检查更新
  5. 使用 BOM:对于多个相关依赖,使用 BOM 统一管理版本
  6. 排除不必要的依赖:减少依赖冲突和安全风险
  7. 使用依赖分析工具:如 Gradle 的 dependencyInsight 任务
标签:Gradle