Dependency Management in Gradle – Gradle中的依赖性管理

最后修改: 2020年 11月 1日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this tutorial, we’ll look at declaring dependencies in a Gradle build script. For our examples, we’ll be using Gradle 6.7.

在本教程中,我们将研究如何在Gradle构建脚本中声明依赖关系。对于我们的例子,我们将使用Gradle 6.7

2. Typical Structure

2.典型结构

Let’s start with a simple Gradle script for Java projects:

让我们从一个简单的Gradle脚本开始,用于Java项目

plugins {
    id 'java'
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter:2.3.4.RELEASE'
    testImplementation 'org.springframework.boot:spring-boot-starter-test:2.3.4.RELEASE'
}

As can be seen above, we have three code blocks: pluginsrepositories, and dependencies.

从上面可以看出,我们有三个代码块。插件资源库依赖关系

First, the plugins block tells us that this is a Java project. Secondly, the dependencies block declares version 2.3.4.RELEASE of the spring-boot-starter dependency needed to compile the project’s production source code. Additionally, it also states that the project’s test suite needs spring-boot-starter-test to compile.

首先,plugins块告诉我们这是个Java项目。其次,dependencies块声明了2.3.4.RELEASE版本的spring-boot-starter依赖项,该依赖项需要编译项目的生产源代码。此外,它还声明项目的测试套件需要spring-boot-starter-test来进行编译。

The Gradle build pulls all dependencies down from the Maven Central repository, as defined by the repositories block.

repositories块所定义的,Gradle构建时从Maven Central仓库拉取所有依赖项。

Let’s focus on how we can define dependencies.

让我们关注一下如何定义依赖关系。

3. Dependency Configurations

3.依赖性配置

There are different configurations in which we can declare dependencies. In this regard, we can choose to be more or less precise, as we’ll see later on.

我们可以用不同的配置来声明依赖关系。在这方面,我们可以选择更多或更少的精确性,这一点我们将在后面看到。

3.1. How To Declare Dependencies

3.1.如何声明依赖关系

To start, the configuration has 4 parts:

首先,该配置有4个部分。

  • group – identifier of an organization, company, or project
  • name – dependency identifier
  • version – the one we want to import
  • classifier – useful to distinguish dependencies with the same groupname, and version

We can declare dependencies in two formats. The contracted format allows us to declare a dependency as a String:

我们可以用两种格式声明依赖关系。契约格式允许我们将依赖关系声明为一个String

implementation 'org.springframework.boot:spring-boot-starter:2.3.4.RELEASE'

implementation 'org.springframework.boot:spring-boot-starter:2.3.4.RELEASE'

Instead, the extended format allows us to write it as a Map:

相反,扩展格式允许我们把它写成Map

implementation group:'org.springframework.boot', name: 'spring-boot-starter', version: '2.3.4.RELEASE'

实现组:'org.springframework.boot', name: 'spring-boot-start', version: '2.3.4.RELEASE'

3.2. Types of Configuration

3.2.配置的类型

Furthermore, Gradle provides many dependencies configuration types:

此外,Gradle提供了许多依赖性配置类型。

  • api – used to make the dependencies explicit and expose them in the classpath. For instance, when implementing a library to be transparent to the library consumers’
  • implementation – required to compile the production source code and are purely internal. They aren’t exposed outside the package
  • compileOnly – used when they need to be declared only at compile-time, such as source-only annotations or annotation processors. They don’t appear in the runtime classpath or the test classpath
  • compileOnlyApi – used when required at compile time and when they need to be visible in the classpath for consumers
  • runtimeOnly – used to declare dependencies that are required only at runtime and aren’t available at compile time
  • testImplementation – required to compile tests
  • testCompileOnly – required only at test compile time
  • testRuntimeOnly – required only at test runtime

We should note that the latest versions of Gradle deprecate some configurations like compile, testCompile, runtime, and testRuntime. At the time of writing, they’re still available.

我们应该注意到,最新版本的Gradle废弃了一些配置,如compiletestCompileruntime,testRuntime.在撰写本文时,它们仍然可用。

4. Types of External Dependencies

4.外部依赖关系的类型

Let’s delve into the types of external dependencies we encounter in a Gradle build script.

让我们深入了解一下我们在Gradle构建脚本中遇到的外部依赖的类型。

4.1. Module Dependencies

4.1.模块的依赖性

Basically, the most common way to declare a dependency is by referencing a repository. A Gradle repository is a collection of modules organized by groupname, and version.

基本上,声明依赖关系最常见的方式是引用一个资源库。Gradle资源库是按版本组织的模块集合。

As a matter of fact, Gradle pulls down the dependencies from the specified repository inside the repository block:

事实上,Gradle从repository块中的指定仓库中提取依赖。

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter:2.3.4.RELEASE'
}

4.2. File Dependencies

4.2.文件的依赖性

Given that projects don’t always use automated dependency management, some projects organize dependencies as part of the source code or the local file system. Thus, we need to specify the exact location where the dependencies are.

鉴于项目并不总是使用自动依赖管理,一些项目将依赖关系组织为源代码或本地文件系统的一部分。因此,我们需要指定依赖关系的确切位置。

For this purpose, we can use files to include a dependency collection:

为此,我们可以使用files来包括一个依赖性集合。

dependencies {
    runtimeOnly files('libs/lib1.jar', 'libs/lib2.jar')
}

Similarly, we can use filetree to include a hierarchy of jar files in a directory:

同样,我们可以使用filetree来包括一个目录中的jar文件的层次结构。

dependencies {
    runtimeOnly fileTree('libs') { include '*.jar' }
}

4.3. Project Dependencies

4.3.项目依赖性

Since one project can depend on another to reuse code, Gradle offers us the opportunity to do so.

由于一个项目可以依赖另一个项目来重用代码,Gradle为我们提供了这样的机会。

Let’s say we want to declare that our project depends on the shared project:

假设我们想声明我们的项目依赖于shared项目。

dependencies { 
    implementation project(':shared') 
}

4.4. Gradle Dependencies

4.4.Gradle的依赖性

In certain cases, such as developing a task or a plugin, we can define dependencies that belong to the Gradle version we are using:

在某些情况下,比如开发一个任务或一个插件,我们可以定义属于我们正在使用的Gradle版本的依赖关系。

dependencies {
    implementation gradleApi()
}

5. buildScript

5.buildScript

As we saw before, we can declare the external dependencies of our source code and tests inside the dependencies block. Similarly, the buildScript block allows us to declare the Gradle build’s dependencies, such as third-party plugins and task classes. Particularly, without a buildScript block, we can use only Gradle out-of-the-box features.

正如我们之前看到的,我们可以在dependencies块中声明我们的源代码和测试的外部依赖。同样地,buildScript块允许我们声明Gradle构建的依赖性,如第三方插件和任务类。特别是,如果没有buildScript块,我们只能使用Gradle开箱即用的功能。

Below we declare that we want to use the Spring Boot plugin by downloading it from Maven Central:

下面我们声明要使用Spring Boot插件,从Maven中心下载该插件。

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'org.springframework.boot:spring-boot-gradle-plugin:2.3.4.RELEASE' 
    }
}
apply plugin: 'org.springframework.boot'

Hence we need to specify the source from which we’ll download external dependencies because there isn’t a default one.

因此,我们需要指定我们将下载外部依赖的来源,因为没有一个默认的来源。

What’s described above is related to older versions of Gradle. Instead, in newer versions, it’s possible to use a more concise form:

上面所描述的是与旧版本的Gradle有关。相反,在较新的版本中,可以使用一种更简洁的形式。

plugins {
    id 'org.springframework.boot' version '2.3.4.RELEASE'
}

6. Conclusion

6.结语

In this article, we looked at Gradle dependencies, how to declare them, and the different configuration types.

在这篇文章中,我们研究了Gradle的依赖性,如何声明它们,以及不同的配置类型。

Given these points, the source code for this article is available over on GitHub.

鉴于以上几点,本文的源代码可在GitHub上获得over