java - Maven:如何覆盖依赖关系

标签 java maven dependency-management

我的情况有点奇怪:

pom 中 Artifact id: yyy 的依赖关系(见下文)具有依赖关系:

<dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>javax.servlet-api</artifactId>
   <version>2.5</version>
</dependency>

所以问题是我需要在当前模块中使用3.1.0版本,因为它有额外的功能:

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
</dependency>

我已经尝试了页面示例中解释的排除标记和依赖管理标记:Maven: how to override the dependency added by a library

它不起作用。我还阅读并尝试了本文中的 3 个示例:https://spring.io/blog/2016/04/13/overriding-dependency-versions-with-spring-boot

它也不起作用。所以我所做的就是重新排序我的 pom 依赖项,以便 3.1.0 位于带有 Artifact yyy 的依赖项之前,我很高兴它成功地构建了一个干净的安装。我的快乐是短暂的,因为在全新安装后,pom 会自行重新排序,并且 3.1.0 会自动重新排序回到 yyy 下面。这意味着下一个构建将再次使用 2.5 并且失败。

我的pom结构片段如下:

<dependencies>
  <dependency>
    <groupId>xxxx.xxx.xxxx</groupId>
    <artifactId>yyy</artifactId>
    <version>1.0.0</version>
  </dependency>
  <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
  </dependency>
<dependencies>

最佳答案

My happiness was short lived because after a clean install the pom re-ordered itself and the 3.1.0 was automatically re-ordered back below the yyy. Which means the next build will use 2.5 again and fail.

请注意,javax.servlet:javax.servlet-api 必须包含在 WAR 中,但只能包含在包含并引导 servlet 容器的独立 JAR 中。
如果您构建标准 WAR,则必须使用服务器提供的依赖项。因此,应该使用 provided 范围来声明依赖项。

I have tried the exclusions tag and dependencymanagement tag explained in the example on the page: Maven: how to override the dependency added by a library

dependencyManagement 在这里将无能为力,因为问题与您在 dependencyManagement 元素之外包含的依赖项有关。
但在 dependency 声明中使用 exclusions 选项是正确的方法。如果以这种方式使用,它应该排除 javax.servlet-api Artifact 的 2.5 版本:

<dependencies>
  <dependency>
    <groupId>xxxx.xxx.xxxx</groupId>
    <artifactId>yyy</artifactId>
    <version>1.0.0</version>
    <exclusions>
      <exclusion>
        <artifactId>javax.servlet</artifactId>
        <groupId>javax.servlet-api</groupId>
      </exclusion>
    </exclusions>
  </dependency>
  <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
  </dependency>
</dependencies>

如果问题仍然存在,则意味着该依赖项可能被另一个依赖项拉动。
一些通常有助于发现这一点的提示:

  • 检查您是否没有使用 WAR 覆盖功能。但这里不太可能,因为您只检索依赖项的 1 个版本

  • 在 WAR 项目上使用 mvn dependency:tree 来检查所有拉取的依赖项。
    为了便于阅读,您还可以通过这种方式进行过滤:
    mvn 依赖项:tree -Dincludes=javax.javax.servlet-api

关于java - Maven:如何覆盖依赖关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51666981/

相关文章:

iPhone 项目依赖管理

java - 如何在Android中构建层次结构?

linux - 如何将 docker 镜像指向我的 .m2 目录,以便在 mac 上的 docker 中运行 maven?

maven - 如何在 Maven 清理阶段删除文件夹?

build - 为什么 Gradle 不在编译/运行时类路径中包含传递依赖项?

groovy - 在 Gradle 中创建多个具有不同依赖项的 .WAR 文件

java - 如何获取Lucene TopDocs中文档的唯一计数?

java - 无法以编程方式从android中的外部存储中删除文件

java - Selenium-Grid:如何使用 `user-extensions.js`

java - 无法将 JDA 导入 Maven - IntelliJ IDEA