我遇到了一些 jar 版本冲突的问题。我依赖于库 group-a:artifact-a:0.0.1
,它依赖于 group-b:artifact-b:0.0.1
,但是我不希望包含 group-b:artifact-b:0.0.1
,因为我知道在运行时会有 group-b:artifact-b:0.0。 2
。
我必须如何编写 pom.xml
文件?
以下哪一项是正确的?它们之间有什么区别?
解决方案一:
从 group-a:artifact-a:0.0.1
中排除 group-b:artifact-b
:
<dependencies>
<dependency>
<groupId>group-a</groupId>
<artifactId>artifact-a</artifactId>
<version>0.0.1</version>
<exclusions>
<exclusion>
<groupId>group-b</groupId>
<artifactId>artifact-b</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
解决方案 2:
添加 group-b:artifact-b
依赖项作为提供:
<dependencies>
<dependency>
<groupId>group-b</groupId>
<artifactId>artifact-b</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>group-a</groupId>
<artifactId>artifact-a</artifactId>
<version>0.0.1</version>
</dependency>
</dependencies>
解决方案 3:
添加 group-b:artifact-b
依赖作为运行时:
<dependencies>
<dependency>
<groupId>group-b</groupId>
<artifactId>artifact-b</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>group-a</groupId>
<artifactId>artifact-a</artifactId>
<version>0.0.1</version>
</dependency>
</dependencies>
解决方案 4:
添加 group-b:artifact-b
依赖作为provided 或 runtime 并将其从 group-a:artifact- 中排除a:0.0.1
.
更新:
抱歉没有明确说明,但是是的,@Tunaki 的假设是正确的,编译时不需要依赖项 group-b:artifact-b:0.0.2
。
最佳答案
选项 5:只需在 POM 中明确声明版本。
如果您在 pom.xml 中显式声明它们,Maven 将覆盖传递依赖项。您不需要对提供的或运行时范围进行任何处理。
根据docs (强调我的):
Dependency mediation - this determines what version of a dependency will be used when multiple versions of an artifact are encountered. Currently, Maven 2.0 only supports using the "nearest definition" which means that it will use the version of the closest dependency to your project in the tree of dependencies. You can always guarantee a version by declaring it explicitly in your project's POM. Note that if two dependency versions are at the same depth in the dependency tree, until Maven 2.0.8 it was not defined which one would win, but since Maven 2.0.9 it's the order in the declaration that counts: the first declaration wins.
所以你的 pom 应该是:
<dependencies>
<dependency>
<groupId>group-b</groupId>
<artifactId>artifact-b</artifactId>
<version>0.0.2</version>
</dependency>
<dependency>
<groupId>group-a</groupId>
<artifactId>artifact-a</artifactId>
<version>0.0.1</version>
</dependency>
</dependencies>
关于java - 我应该如何排除提供的 jar 的错误版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32275646/