Skip to content

统一依赖版本管理

在大型多模块项目中,统一管理所有模块的依赖版本是避免版本冲突的关键。

完整的 libs.versions.toml

toml
# gradle/libs.versions.toml

[versions]
# 构建工具
agp = "8.2.1"
kotlin = "1.9.22"

# Spring 生态
spring-boot = "3.2.0"
spring-cloud = "2023.0.0"
spring-dependency-management = "1.1.4"

# 持久化
mybatis-spring-boot = "3.0.3"
flyway = "10.4.1"
hibernate-validator = "8.0.1.Final"

# 网络与序列化
jackson = "2.15.3"
okhttp = "4.12.0"
retrofit = "2.9.0"

# 工具库
guava = "32.0.1-jre"
commons-lang3 = "3.14.0"
mapstruct = "1.5.5.Final"
lombok = "1.18.30"

# 日志
slf4j = "2.0.9"
logback = "1.4.14"

# 测试
junit = "5.10.1"
mockito = "5.8.0"
assertj = "3.25.1"
testcontainers = "1.19.3"

[libraries]
# Spring Boot
spring-boot-web = { module = "org.springframework.boot:spring-boot-starter-web" }
spring-boot-jpa = { module = "org.springframework.boot:spring-boot-starter-data-jpa" }
spring-boot-redis = { module = "org.springframework.boot:spring-boot-starter-data-redis" }
spring-boot-security = { module = "org.springframework.boot:spring-boot-starter-security" }
spring-boot-actuator = { module = "org.springframework.boot:spring-boot-starter-actuator" }
spring-boot-test = { module = "org.springframework.boot:spring-boot-starter-test" }
spring-boot-bom = { module = "org.springframework.boot:spring-boot-dependencies", version.ref = "spring-boot" }

# 数据库
mybatis = { module = "org.mybatis.spring.boot:mybatis-spring-boot-starter", version.ref = "mybatis-spring-boot" }
flyway-core = { module = "org.flywaydb:flyway-core", version.ref = "flyway" }
flyway-mysql = { module = "org.flywaydb:flyway-mysql", version.ref = "flyway" }
mysql = { module = "com.mysql:mysql-connector-j" }
h2 = { module = "com.h2database:h2" }

# 工具
guava = { module = "com.google.guava:guava", version.ref = "guava" }
commons-lang3 = { module = "org.apache.commons:commons-lang3", version.ref = "commons-lang3" }
mapstruct = { module = "org.mapstruct:mapstruct", version.ref = "mapstruct" }
mapstruct-processor = { module = "org.mapstruct:mapstruct-processor", version.ref = "mapstruct" }
lombok = { module = "org.projectlombok:lombok", version.ref = "lombok" }

# 测试
junit-bom = { module = "org.junit:junit-bom", version.ref = "junit" }
junit-jupiter = { module = "org.junit.jupiter:junit-jupiter" }
junit-launcher = { module = "org.junit.platform:junit-platform-launcher" }
mockito-core = { module = "org.mockito:mockito-core", version.ref = "mockito" }
mockito-junit = { module = "org.mockito:mockito-junit-jupiter", version.ref = "mockito" }
assertj = { module = "org.assertj:assertj-core", version.ref = "assertj" }
testcontainers-bom = { module = "org.testcontainers:testcontainers-bom", version.ref = "testcontainers" }
testcontainers-junit = { module = "org.testcontainers:junit-jupiter" }
testcontainers-mysql = { module = "org.testcontainers:mysql" }

[bundles]
# 常用依赖组合
spring-web = ["spring-boot-web", "spring-boot-actuator"]
spring-data = ["spring-boot-jpa", "mysql"]
lombok-mapstruct = ["lombok", "mapstruct"]
testing = ["junit-jupiter", "mockito-core", "mockito-junit", "assertj"]
testcontainers = ["testcontainers-junit", "testcontainers-mysql"]

[plugins]
spring-boot = { id = "org.springframework.boot", version.ref = "spring-boot" }
spring-dependency = { id = "io.spring.dependency-management", version.ref = "spring-dependency-management" }
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
kotlin-spring = { id = "org.jetbrains.kotlin.plugin.spring", version.ref = "kotlin" }
flyway = { id = "org.flywaydb.flyway", version.ref = "flyway" }

使用版本目录

kotlin
// 任意子项目 build.gradle.kts
plugins {
    alias(libs.plugins.spring.boot)
    alias(libs.plugins.spring.dependency)
}

dependencies {
    implementation(platform(libs.spring.boot.bom))
    
    // 单个依赖
    implementation(libs.spring.boot.web)
    implementation(libs.guava)
    
    // Bundle(一次引入多个相关依赖)
    implementation(libs.bundles.spring.web)
    compileOnly(libs.lombok)
    annotationProcessor(libs.lombok)
    annotationProcessor(libs.mapstruct.processor)
    
    testImplementation(libs.bundles.testing)
}

下一步