我的情况有点奇怪:
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/