全面了解Maven依赖范围

2023-11-03

Maven依赖范围

翻译原文详见 Maven Dependency Scopes

1. 总览

Maven 是 Java 生态系统中最流行的构建工具之一,其核心特性之一是依赖管理。

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

在本教程中,我们将描述和探索有助于管理 Maven 项目中的传递依赖关系的机制——依赖范围

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

2. 传递依赖

Maven 中有两种依赖类型:直接(依赖)和 传递(依赖)

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

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

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

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

These can be included using <dependency> tags:

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

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

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

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

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

3. 依赖范围

依赖范围可以帮助限制依赖的传递性。他们还为不同的构建任务修改类路径。 Maven 有 六个 默认的依赖范围。

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.

(我们下面)重要的是要了解每个(依赖)范围(除了导入)都会对传递依赖项产生影响。

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

3.1. Compile

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

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.

更重要的是,这些依赖关系也是传递的:

More importantly, these dependencies are also transitive:

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

3.2. Provided

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

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

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

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.

在我们的项目中,我们可以使用 provided 范围来定义这些依赖项:

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

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

这 provided 的依赖项仅在编译时和项目的测试类路径中可用。这些依赖关系也不是传递的。

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

3.3. Runtime

运行时需要具有此范围的依赖项。但是我们不需要它们来编译项目代码。因此,标有 runtime 范围的依赖项将出现在运行时和测试类路径中,但它们将在编译类路径中丢失。

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.

JDBC 驱动程序是一个很好的依赖关系示例,它应该使用 runtime 范围:

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

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

3.4. Test

我们使用此范围来指示在应用程序的标准运行时不需要依赖项,而仅用于测试目的。

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.

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

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

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

3.5. System

system 范围与 provided 范围非常相似。主要区别在于 system 要求我们直接指向系统上的特定jar。

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 范围已被弃用。

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

(这里)重点是要记住,如果依赖项不存在或位于与 systemPath 指向的位置不同的位置,则使用系统范围依赖项构建项目可能会在不同的机器上失败:

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:

<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

它仅适用于依赖类型 pom。

It’s only available for the dependency type pom.

import 表示此依赖项应替换为其 POM 中声明的所有有效依赖项。

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

在这里,下面的自定义项目依赖项将替换为自定义项目的 pom.xml <dependencyManagement> 部分中声明的所有依赖项。

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

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

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.

但是,provided 和 test 范围的依赖项永远不会包含在主项目中。

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

让我们详细看看这意味着什么:

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

对于 compile 范围,所有具有 runtime 范围的依赖都会被拉入到项目中的 runtime 范围中,所有具有 compile 范围的依赖都会被拉入到项目中的 compile 范围中。

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.

对于 provided 范围,runtime 和 compile 范围的依赖项都将与项目中 provided 范围一起引入。

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

对于 test 范围,runtime 和 compile 范围的传递依赖都将与项目中的 test 范围一起引入。

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

对于 runtime 范围,runtime 和 compile 范围传递依赖项都将与项目中的 runtime 范围一起引入。

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

下面看下依赖范围图表

依赖范围 编译 测试 运行时 是否被打包
compile
provided × ×
runtime ×
test × × ×
system ×

5. 总结

在这篇快速文章中,我们专注于 Maven 依赖范围、它们的用途以及它们如何操作的细节。

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

全面了解Maven依赖范围 的相关文章

随机推荐

  • C++硬币问题

    include
  • Unity3D引用dll打包发布的问题及解决

    今年我们开始使用Unity3D开发MMORPG 脚本语言使用C 这样我们就可以使用以往积累的许多类库 但是 在U3D中使用 NET dll的过程并不是那么顺利 比如我们今天遇到的这种问题 一 问题出现 我们在当前的一个U3D项目中使用了St
  • gin框架23--绑定 HTML 复选框

    gin框架23 绑定 HTML 复选框 介绍 案例 说明 介绍 本文通过一个简单的案例 将结构体和html中的 form 数据绑定在一起 案例 源码 main go package main import github com gin go
  • 裸设备和Oracle问答20例

    1 什么叫做裸设备 裸设备 也叫裸分区 原始分区 是一种没有经过格式化 不被Unix通过文件系统来读取的特殊字符设备 它由应用程序负责对它进行读写操作 不经过文件系统的缓冲 2 如何辨别裸设备 在Unix的 dev 目录下 有许多文件 其中
  • 【C++设计模式】开放-封闭原则

    2023年8月27日 周日下午 我觉得我的这篇博客还是写得很不错的 哈哈哈 目录 概述 举例说明 用开放 封闭原则重构 概述 开放 封闭原则 Open Closed Principle OCP 是面向对象设计中的一个重要原则 也是许多设计模
  • shineblink CoreTFT串口屏开发

    TFT液晶触摸屏 一 开发基础准备工作 二 本章节实现功能介绍 三 接线图 四 开发板端完整代码 五 液晶屏页面的开发 六 代码运行结果 Shineblink Core 可支持3 5寸 4寸 7寸的TFT串口彩色液晶屏 本篇章主要演示了TJ
  • Windows 常用快捷键

    常用快捷键 Ctrl C 复制选定项 Ctrl X 剪切选定项 Ctrl V 粘贴选定项 Ctrl Z 撤消操作 Ctrl Y 重做操作 Ctrl S 保存 Ctrl A 选择文档或窗口中的所有项目 Alt Tab 在打开的应用之间切换 W
  • Qt Utils : To-Do

    Qt Creator自带的todo插件工具 真心舒爽 特别是对于我这种记不住三天前自己写的shit mountain的 渣渣CXY来讲 边撸代码边注释 快速查阅Task 非常重要 1 上效果图 2 工具使用 1 勾选使用插件 重启Qt Cr
  • 7-32 统计MOOC证书

    本题要求编写程序 输入N个学生的MOOC成绩 统计优秀 合格证书的数量 以及没有获得证书的数量 学生修读程序设计MOOC 85分及以上获得优秀证书 不到85分但是60分及以上获得合格证书 不到60分则没有证书 输入格式 输入在第一行中给出非
  • 如何安装新的PHP扩展模块

    一 phpize方式 该方法用于安装php源码ext目录中没有的扩展 1 下载源码 2 解压并进入扩展目录 3 执行phpize 4 执行 configure 5 make make install 6 在php ini 文件中添加 ext
  • JAVA识别复杂验证码+图像处理

    先对验证码进行简单的处理噪点和纯色 例未曾处理的图片 public static void main String args throws Exception 源文件 String picName C Users syxy101 Deskt
  • 您无权输入许可证密钥,请请使用系统管理员账户重试

    转载于 https www cnblogs com java2016 p 5448093 html
  • Android 解决Retrofit请求数据,数据过多,返回json数据乱码问题

    同一个POST请求返回的数据 有时候乱码 有时候不乱码 最后发现 数据量过多就会出现乱码 直接使用原生HttpUrlConnection请求不会产出乱码 我猜测就是框架配置的问题 然后在网上寻找解决方面发现一个帖子类似我这种问题 地址 问题
  • 小米9 MIUI12.5 红米 K40s MIUI13.0.10 安装谷歌框架

    前言 由于更换了小米9手机后 我想重新安装谷歌框架的 发现这谷歌框架安装不上了 下载第三方安装框架工具也是不行 然后看到一篇文章说小米9手机自带有个GMS服务框架 然后我试了一下还真的可以 所以分享一下 如果文章没了说明我已经进去了 设备
  • 【OJ刷题

    欢迎关注微信公众号 Python小灶 和我一起每天学习Python新知识 还可添加博主Vx yf03064131 方便一起交流学习 或者B站搜索 有只小新 文章目录 多数组合并 数组排序 最长的指定瑕疵度的元音字串 20210909 总共三
  • 2016的诗和远方

    当生活被眼前的苟且蒙住了双眼 其实你所希冀的远方 就在那噼啪敲击的键盘里 渗入脑海实现想法的一行行代码中 眼看一年就要结束了 坐在公司的办公区里 窗外霓虹灯五光十色 办公楼鳞次栉比 恍惚想起了这一年充满波折又熠熠生辉的日子 却又是欲买桂花同
  • 新建文件夹命令 linux,linux创建文件夹命令是什么

    linux创建文件夹命令是mkdir 语法为 mkdir 选项 参数 其中Z为设置安全上下文 当使用SELinux时有效 version显示版本信息 本文操作环境 linux2 6 32系统 DELL G3电脑 Linux中 mkdir 创
  • CEVA RivieraWaves™ Wi-Fi IP

    https www ceva dsp com product rivierawaves wi fi platforms OVERVIEW The RivieraWaves Wi Fi IP family is a comprehensive
  • 提交本地项目到GitHub

    文章目录 1 下载git 1 1 通过homebrew安装Git 1 2 通过Xcode安装 2 创建ssh key 配置git 3 提交本地项目到GitHub 说明 该博文参考这篇文章和这段视频 1 下载git 1 1 通过homebre
  • 全面了解Maven依赖范围

    Maven依赖范围 翻译原文详见 Maven Dependency Scopes 1 总览 Maven 是 Java 生态系统中最流行的构建工具之一 其核心特性之一是依赖管理 Maven is one of the most popular