原文: https://howtodoinjava.com/maven/maven-dependency-management/

在 Maven 中,依赖项是您当前项目为了编译,构建,测试和/或运行而需要的另一个归档文件(JAR,ZIP 等)。 依赖项收集在<dependency>标签内的pom.xml文件中。

当您运行构建或执行 Maven 目标时,将解决这些依赖项,然后从本地仓库加载这些依赖项。 如果它们不存在,那么 Maven 将从远程仓库下载它们并将它们存储在本地仓库中。 您也可以手动安装依赖项。

了解更多:本地仓库路径

  1. Table of Contents
  2. Dependency Example
  3. External Dependency
  4. Dependency Tree
  5. Dependency Exclusion
  6. Artifact Version Ranges

Maven 依赖示例

在深入研究依赖管理之前,让我们看一个简单的示例,其中包含不同的元素,我们将在本文中讨论。

  1. <dependencies>
  2. <dependency>
  3. <groupId>junit</groupId>
  4. <artifactId>junit</artifactId>
  5. <version>4.12</version>
  6. <scope>test</scope>
  7. </dependency>
  8. <dependency>
  9. <groupId>org.springframework</groupId>
  10. <artifactId>spring-core</artifactId>
  11. <version>4.3.5.RELEASE</version>
  12. </dependency>
  13. </dependencies>

如果pom.xml指向同一groupId的许多工件,则应使用属性以便分解代码以便于维护。

  1. <properties>
  2. <junit.version>4.12</junit.version>
  3. <spring.version>4.3.5.RELEASE</spring.version>
  4. </properties>
  5. <dependencies>
  6. <dependency>
  7. <groupId>junit</groupId>
  8. <artifactId>junit</artifactId>
  9. <version>${junit.version}</version>
  10. <scope>test</scope>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.springframework</groupId>
  14. <artifactId>spring-core</artifactId>
  15. <version>${spring.version}</version>
  16. </dependency>
  17. </dependencies>

外部依赖

有时,您将必须引用不在 maven 仓库(本地,中央或远程仓库)中的 jar 文件。 您可以通过将这些 jar 放在项目的lib文件夹中来使用它们,并配置外部依赖项,如下所示:

  1. <dependency>
  2. <groupId>extDependency</groupId>
  3. <artifactId>extDependency</artifactId>
  4. <scope>system</scope>
  5. <version>1.0</version>
  6. <systemPath>${basedir}\war\WEB-INF\lib\extDependency.jar</systemPath>
  7. </dependency>
  • groupIdartifactId都设置为依赖项的名称。
  • scope元素值设置为system
  • systemPath元素引用 JAR 文件的位置。

Maven 依赖树

使用 maven 的dependency:tree命令,可以传递地查看项目中所有依赖项的列表。 传递依赖表示如果 A 依赖于 B 而 B 依赖于 C,则 A 依赖于 B 和 C。

当不同依赖项包含相同工件的不同版本时,传递性带来了一个非常严重的问题。 它可能会在运行时导致版本不匹配问题。 在这种情况下,dependency:tree命令对于处理 JAR 冲突非常有用。

  1. $ mvn dependency:tree

它以给定的格式输出依赖项信息:

  1. [INFO] --- maven-dependency-plugin:2.1:tree (default-cli) @ MavenExamples ---
  2. [INFO] com.howtodoinjava.demo:MavenExamples:jar:0.0.1-SNAPSHOT
  3. [INFO] +- junit:junit:jar:3.8.1:test
  4. [INFO] \- org.springframework:spring-core:jar:4.3.5.RELEASE:compile
  5. [INFO] \- commons-logging:commons-logging:jar:1.2:compile

了解它如何通知 spring 依赖commons-logging。 同样,您可以使用此命令获取完整的传递依赖项信息。

Maven 依赖排除

除了由传递依赖引起的版本不匹配问题之外,项目工件和部署平台(例如 Tomcat 或其他服务器)之间的工件之间可能存在版本不匹配

为了解决此类版本不匹配的问题,maven 提供了<exclusion>标记,以打破传递依赖项。

例如,当您在类路径中具有 JUnit4.12 并包括 DBUnit 依赖项时,则需要删除 JUnit 3.8.2 依赖项。 可以使用exclusion标签完成。

  1. <dependency>
  2. <groupId>junit</groupId>
  3. <artifactId>junit</artifactId>
  4. <version>${junit.version}</version>
  5. <scope>test</scope>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.dbunit</groupId>
  9. <artifactId>dbunit</artifactId>
  10. <version>${dbunit.version}</version>
  11. <scope>test</scope>
  12. <exclusions>
  13. <!--Exclude transitive dependency to JUnit-3.8.2 -->
  14. <exclusion>
  15. <artifactId>junit</artifactId>
  16. <groupId>junit</groupId>
  17. </exclusion>
  18. </exclusions>
  19. </dependency>

工件版本范围

在包含依赖项的同时,您可以自由地为任何工件指定版本范围。 要给出版本范围,可以使用以下符号:

  • 括号符号()表示包含范围
  • 括号符号[]表示排除范围
  • 逗号分隔的子集

版本范围示例

让我们看一些示例,以更好地了解有关指定版本范围的信息。

范围 含义
1.2 等于 1.2 或以 1.2 开头的版本
(,1.2] 小于 1.2 的任何版本。 包含 1.2 版。 x <= 1.2
(,1.2) 小于 1.2 的任何版本。 1.2 版除外。 x < 1.2
[1.2] 仅限于 1.2 版。 x == 1.0
[1.2,) 任何大于 1.2 的版本。 包含 1.2 版。 x >= 1.2
(1.2,) 任何大于 1.2 的版本。 1.2 版除外。 x > 1.2
(1.2,2.2) 在 1.2 和 2.2 之间的版本。 两者都排除在外。 1.0 < x < 2.0
[1.2,2.2] 在 1.2 和 2.2 之间的版本。 两者都包括在内。 1.2 <= x <= 2.2
(,1.2],[2.2,) 小于 1.2 或大于 2.2 的版本。 两者都包括在内。 x <= 1.2 or x >= 2.2

将我的问题放在评论部分。

学习愉快!