Maven Dependency Scopes – Maven的依赖范围

最后修改: 2018年 3月 15日

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

1. Overview

1.概述

Maven is one of the most popular build tools in the Java ecosystem, and one of its core features is dependency management.

Maven是Java生态系统中最受欢迎的构建工具之一,其核心功能之一是依赖性管理。

In this tutorial, we’re going to describe and explore the mechanism that helps in managing transitive dependencies in Maven projects — dependency scopes.

在本教程中,我们将描述并探讨有助于管理Maven项目中的横向依赖关系的机制–依赖范围。

2. Transitive Dependency

2.传递性依赖

There are two types of dependencies in Maven: direct and transitive.

Maven有两种类型的依赖关系:直接依赖和传递性依赖。

Direct dependencies are the ones that we explicitly include in the project.

直接依赖是指我们明确包含在项目中的那些依赖。

These can be included using <dependency> tags:

这些可以使用<dependency>标签包含。

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
</dependency>

On the other hand, transitive dependencies are required by direct dependencies. Maven automatically includes required transitive dependencies in our project.

另一方面,反式依赖是直接依赖所要求的。Maven会在我们的项目中自动包含所需的反式依赖。

We can list all dependencies including transitive dependencies in the project using mvn dependency:tree command.

我们可以使用mvn dependency:tree命令列出项目中的所有依赖关系,包括反式依赖关系。

3. Dependency Scopes

3.依赖范围

Dependency scopes can help to limit the transitivity of the dependencies. They also modify the classpath for different build tasks. Maven has six default dependency scopes.

依赖范围可以帮助限制依赖关系的反义性。它们还能为不同的构建任务修改classpath。Maven有六个默认的依赖作用域

And it’s important to understand that each scope — except for import — has an impact on transitive dependencies.

而且必须理解每个作用域–除了import–都会对横向依赖关系产生影响。

3.1. Compile

3.1.编译

This is the default scope when no other scope is provided.

当没有提供其他范围时,这是默认的范围。

Dependencies with this scope are available on the classpath of the project in all build tasks. They are also propagated to the dependent projects.

在所有的构建任务中,项目的classpath上都有这个范围的依赖项。它们也会被传播到依赖的项目中。

More importantly, these dependencies are also transitive:

更重要的是,这些依赖关系也是跨度的。

<dependency>
    <groupId>commons-lang</groupId>
    <artifactId>commons-lang</artifactId>
    <version>2.6</version>
</dependency>

3.2. Provided

3.2.提供了

We use this scope to mark dependencies that should be provided at runtime by JDK or a container.

我们使用这个范围来标记应该由JDK或容器在运行时提供的依赖。

A good use case for this scope would be a web application deployed in some container, where the container already provides some libraries itself. For example, this could be a web server that already provides the Servlet API at runtime.

这个范围的一个很好的用例是部署在一些容器中的Web应用,容器本身已经提供了一些库。例如,这可能是一个已经在运行时提供Servlet API的Web服务器。

In our project, we can define those dependencies with the provided scope:

在我们的项目中,我们可以用provided范围定义这些依赖关系。

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>4.0.1</version>
    <scope>provided</scope>
</dependency>

The provided dependencies are available only at compile time and in the test classpath of the project. These dependencies are also not transitive.

提供的依赖项只在编译时和项目的测试classpath中可用。这些依赖关系也不是反式的。

3.3. Runtime

3.3.运行时间

The dependencies with this scope are required at runtime. But we don’t need them for the compilation of the project code. Because of that, dependencies marked with the runtime scope will be present in the runtime and test classpath, but they will be missing from the compile classpath.

这个范围的依赖在运行时是需要的。但是我们在编译项目代码时不需要它们。正因为如此,标有runtime范围的依赖将出现在运行时和测试classpath中,但它们将在编译classpath中消失。

A JDBC driver is a good example of dependencies that should use the runtime scope:

JDBC 驱动程序是应该使用运行时范围的依赖关系的一个好例子。

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.28</version>
    <scope>runtime</scope>
</dependency>

3.4. Test

3.4.测试

We use this scope to indicate that dependency isn’t required at standard runtime of the application but is used only for test purposes.

我们用这个范围来表示依赖性在应用程序的标准运行时是不需要的,而只是用于测试目的。

Test dependencies aren’t transitive and are only present for test and execution classpaths.

测试依赖关系不是传递性的,只存在于测试和执行classpaths。

The standard use case for this scope is adding a test library such as JUnit to our application:

这个范围的标准用例是向我们的应用程序添加一个测试库,如JUnit。

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>

3.5. System

3.5.系统

System scope is very similar to the provided scope. The main difference is that system requires us to directly point to a specific jar on the system.

System作用域与provided作用域非常相似。主要区别在于,system要求我们直接指向系统上的一个特定jar。

It’s worthwhile to mention that system scope is deprecated.

值得一提的是,system范围已被废弃

The important thing to remember is that building the project with system scope dependencies may fail on different machines if dependencies aren’t present or are located in a different place than the one systemPath points to:

需要记住的是,如果依赖关系不存在,或者位于与systemPath指向的位置不同的地方,用system范围依赖关系构建项目在不同的机器上可能会失败。

<dependency>
    <groupId>com.baeldung</groupId>
    <artifactId>custom-dependency</artifactId>
    <version>1.3.2</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/libs/custom-dependency-1.3.2.jar</systemPath>
</dependency>

3.6. Import

3.6.进口

It’s only available for the dependency type pom.

它只适用于依赖类型pom

import indicates that this dependency should be replaced with all effective dependencies declared in its POM.

import表示该依赖应被替换成其POM中声明的所有有效依赖。

Here, below custom-project dependency will be replaced with all dependencies declared in custom-project’s pom.xml <dependencyManagement> section.

这里,下面的custom-project依赖将被替换成custom-project的pom.xml <dependencyManagement>部分中声明的所有依赖。

<dependency>
    <groupId>com.baeldung</groupId>
    <artifactId>custom-project</artifactId>
    <version>1.3.2</version>
    <type>pom</type>
    <scope>import</scope>
</dependency>

4. Scope and Transitivity

4.范围和跨度

Each dependency scope affects transitive dependencies in its own way. This means that different transitive dependencies may end up in the project with different scopes.

每一个依赖范围都以自己的方式影响着交叉性依赖。这意味着不同的横向依赖可能在项目中以不同的作用域结束。

However, dependencies with scopes provided and test will never be included in the main project.

然而,作用域为providedtest的依赖项将永远不会被包含在主项目中。

Let’s take a detailed look at what this means:

让我们详细看一下这意味着什么。

  • For the compile scope, all dependencies with runtime scope will be pulled in with the runtime scope in the project, and all dependencies with the compile scope will be pulled in with the compile scope in the project.
  • For the provided scope, both runtime and compile scope dependencies will be pulled in with the provided scope in the project.
  • For the test scope, both runtime and compile scope transitive dependencies will be pulled in with the test scope in the project.
  • For the runtime scope, both runtime and compile scope transitive dependencies will be pulled in with the runtime scope in the project.

5. Conclusion

5.结论

In this quick article, we focused on Maven dependency scopes, their purpose and the details of how they operate.

在这篇简短的文章中,我们着重介绍了Maven的依赖范围,其目的和操作细节。

To dig deeper into Maven, the documentation is a great place to start.

要深入了解Maven,文档是一个不错的开始。