在 Gradle 中声明依赖涉及指定项目所依赖的库或文件。
理解生产者和消费者
在依赖管理中,理解生产者和消费者之间的区别至关重要。
当你构建一个库时,你扮演着生产者的角色,创建将被他人(即消费者)消费的制品。
当你依赖该库时,你扮演着消费者的角色。消费者可以广义地定义为
-
依赖其他项目的项目。
-
声明对特定制品的依赖的配置。
我们在依赖管理中做出的决策通常取决于我们正在构建的项目类型,特别是我们是哪种类型的消费者。

添加依赖
要在 Gradle 中添加依赖,可以在构建脚本中使用 dependencies{}
块。
dependencies
块允许你指定各种类型的依赖,例如外部库、本地 JAR 文件或多项目构建中的其他项目。
Gradle 中的外部依赖使用配置名称(例如,implementation
、compileOnly
、testImplementation
)声明,后跟依赖符号,其中包含 group ID (group)、artifact ID (name) 和版本。
dependencies {
// Configuration Name + Dependency Notation - GroupID : ArtifactID (Name) : Version
configuration('<group>:<name>:<version>')
}
注意
理解依赖的类型
依赖有三种类型:模块依赖、项目依赖和文件依赖。
1. 模块依赖
模块依赖是最常见的依赖。它们指代仓库中的一个模块
dependencies {
implementation("org.codehaus.groovy:groovy:3.0.5")
implementation("org.codehaus.groovy:groovy-json:3.0.5")
implementation("org.codehaus.groovy:groovy-nio:3.0.5")
}
dependencies {
implementation 'org.codehaus.groovy:groovy:3.0.5'
implementation 'org.codehaus.groovy:groovy-json:3.0.5'
implementation 'org.codehaus.groovy:groovy-nio:3.0.5'
}
2. 项目依赖
项目依赖允许你声明对同一构建中的其他项目的依赖。这在多个项目属于同一 Gradle 构建的多项目构建中非常有用。
项目依赖通过引用项目路径来声明
dependencies {
implementation(project(":utils"))
implementation(project(":api"))
}
dependencies {
implementation project(':utils')
implementation project(':api')
}
3. 文件依赖
在某些项目中,你可能不依赖 JFrog Artifactory 或 Sonatype Nexus 等二进制仓库产品来托管和解析外部依赖。相反,你可能会将这些依赖托管在共享驱动器上或将其与项目源代码一起检入版本控制系统。
这些被称为文件依赖,因为它们表示没有附加任何元数据(例如关于传递性依赖、来源或作者的信息)的文件。

要将文件作为配置的依赖添加,只需传递一个文件集合作为依赖即可
dependencies {
runtimeOnly(files("libs/a.jar", "libs/b.jar"))
runtimeOnly(fileTree("libs") { include("*.jar") })
}
dependencies {
runtimeOnly files('libs/a.jar', 'libs/b.jar')
runtimeOnly fileTree('libs') { include '*.jar' }
}
建议优先使用项目依赖或外部依赖,而不是文件依赖。 |
查看示例
让我们想象一个使用 Guava 的 Java 应用示例,Guava 是一套来自 Google 的核心 Java 库

该 Java 应用包含以下 Java 类
package org.example;
import com.google.common.collect.ImmutableMap; // Comes from the Guava library
public class InitializeCollection {
public static void main(String[] args) {
ImmutableMap<String, Integer> immutableMap
= ImmutableMap.of("coin", 3, "glass", 4, "pencil", 1);
}
}
要将 Guava 库作为依赖添加到你的 Gradle 项目中,必须在构建文件中添加以下行
dependencies {
implementation("com.google.guava:guava:23.0")
}
dependencies {
implementation 'com.google.guava:guava:23.0'
}
其中
-
implementation
是配置。 -
com.google.guava:guava:23.0
指定了库的 group、name 和版本-
com.google.guava
是 group ID。 -
guava
是制品 ID(即 name)。 -
23.0
是版本。
-
快速查看 Maven Central 中的 Guava 页面作为参考。
列出项目依赖
dependencies
任务提供了项目依赖的概览。它通过从命令行渲染依赖树来帮助你理解使用了哪些依赖、它们如何被解析以及它们之间的关系,包括任何传递性依赖。
此任务对于调试依赖问题特别有用,例如版本冲突或缺少依赖。
例如,假设我们的 app
项目的构建脚本中包含以下行
dependencies {
implementation("com.google.guava:guava:30.0-jre")
runtimeOnly("org.apache.commons:commons-lang3:3.14.0")
}
dependencies {
implementation("com.google.guava:guava:30.0-jre")
runtimeOnly("org.apache.commons:commons-lang3:3.14.0")
}
在 app
项目上运行 dependencies
任务会得到以下结果
$ ./gradlew app:dependencies > Task :app:dependencies ------------------------------------------------------------ Project ':app' ------------------------------------------------------------ implementation - Implementation dependencies for the 'main' feature. (n) \--- com.google.guava:guava:30.0-jre (n) runtimeClasspath - Runtime classpath of source set 'main'. +--- com.google.guava:guava:30.0-jre | +--- com.google.guava:failureaccess:1.0.1 | +--- com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava | +--- com.google.code.findbugs:jsr305:3.0.2 | +--- org.checkerframework:checker-qual:3.5.0 | +--- com.google.errorprone:error_prone_annotations:2.3.4 | \--- com.google.j2objc:j2objc-annotations:1.3 \--- org.apache.commons:commons-lang3:3.14.0 runtimeOnly - Runtime-only dependencies for the 'main' feature. (n) \--- org.apache.commons:commons-lang3:3.14.0 (n)
我们可以清楚地看到,对于 implementation
配置,已添加 com.google.guava:guava:30.0-jre
依赖。对于 runtimeOnly
配置,已添加 org.org.apache.commons:commons-lang3:3.14.0
依赖。
我们还看到 com.google.guava:guava:30.0-jre
的传递性依赖列表(即 guava
库的依赖),例如在 runtimeClasspath
配置中的 com.google.guava:failureaccess:1.0.1
。
下一步: 了解依赖配置 >>