Appearance
声明依赖
基本语法
kotlin
dependencies {
// 字符串形式(最常用)
implementation("com.google.guava:guava:32.0.1-jre")
// Map 形式(明确分组)
implementation(group = "com.google.guava", name = "guava", version = "32.0.1-jre")
// 版本目录引用(推荐)
implementation(libs.guava)
}版本范围
kotlin
dependencies {
// 精确版本(推荐)
implementation("com.google.guava:guava:32.0.1-jre")
// 动态版本(不推荐生产环境使用)
implementation("com.google.guava:guava:latest.release") // 最新发布版
implementation("com.google.guava:guava:32.+") // 32.x 最新
implementation("com.google.guava:guava:[30,33)") // 30 到 33(不含)
// 快照版本
implementation("com.example:my-lib:1.0.0-SNAPSHOT")
}版本声明策略
kotlin
dependencies {
implementation("com.google.guava:guava") {
version {
strictly("[30, 33)") // 严格范围
prefer("32.0.1-jre") // 首选版本
reject("31.0.0-jre") // 拒绝版本
}
}
}排除依赖
kotlin
dependencies {
// 排除某个传递依赖
implementation("org.springframework.boot:spring-boot-starter-web:3.2.0") {
exclude(group = "org.springframework.boot", module = "spring-boot-starter-tomcat")
}
// 排除所有 commons-logging
configurations.all {
exclude(group = "commons-logging")
}
}
// 添加替代依赖
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web:3.2.0") {
exclude(group = "org.springframework.boot", module = "spring-boot-starter-tomcat")
}
implementation("org.springframework.boot:spring-boot-starter-jetty:3.2.0")
}Classifier(分类器)
kotlin
dependencies {
// 使用 tests classifier(测试 JAR)
testImplementation("com.example:my-lib:1.0:tests")
// 使用 sources classifier
implementation("com.example:my-lib:1.0:sources")
}依赖能力(Capabilities)
kotlin
dependencies {
// 声明能力需求
implementation("com.example:my-lib:1.0") {
capabilities {
requireCapability("com.example:my-lib-optional-feature:1.0")
}
}
}平台依赖(BOM)
kotlin
dependencies {
// 导入 Spring Boot BOM,统一管理版本
implementation(platform("org.springframework.boot:spring-boot-dependencies:3.2.0"))
// 使用 BOM 管理的依赖,无需指定版本
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
}文件依赖
kotlin
dependencies {
// 单个文件
implementation(files("libs/custom.jar"))
// 文件树
implementation(fileTree("libs") { include("*.jar") })
// 运行时添加到 classpath
runtimeOnly(files("$buildDir/generated-classes"))
}