如何停止在Maven中覆盖传递依赖的版本?
问题描述:
TL; DR如何停止在Maven中覆盖传递依赖的版本?
我有一个依赖管理问题。我想简化我的问题的说明,所以我没有要发布的NoClassDefFoundError的全堆栈跟踪:.. VeryImportantStuff等
简单地说,我用2依赖性:A-1.1和B- 1.0我的项目C。我不能接受A或B的另一个版本。 B-1.0取决于A-1.0。 A的作者不尊重后向兼容性规则,A库的最新版本(1.1)根本没有类VeryImportantStuff.class
。
<dependency>
<groupId>org.thirdparty.lib</groupId>
<artifactId>A</artifactId>
<version>1.1</version> <!-- overrides B->A version !!! (((-->
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.another.thirdparty.lib</groupId>
<artifactId>B</artifactId>
<version>1.0</version>
<scope>test</scope>
<!--
<dependencyManagement>
<dearMavenIbegYouPleaseUseThisDependency>
<dependency>
<groupId>org.thirdparty.lib</groupId>
<artifactId>A</artifactId>
<version>1.0</version> PLEASE!!!
</dependency>
</dearMavenIbegYouPleaseUseThisDependency>
</dependencyManagement>
-->
</dependency>
问题
如何分辨乙(或我可以添加到我的pom.xml的),它应该使用1.0版本A的,不属于其规定的1.1 pom.xml考虑到我的应用程序代码应该使用A-1.1?
答
只需向依赖项B
添加排除,并告诉您不需要A
。然后添加A
版本1.1作为您的依赖关系。
<dependency>
<groupId>org.another.thirdparty.lib</groupId>
<artifactId>B</artifactId>
<version>1.0</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.thirdparty.lib</groupId>
<artifactId>A</artifactId>
</exclusion>
</exclusions>
</dependency>
如果* ... B-1.0依赖于A-1.0 *然后B-1.0是建立使用A的1.0只,为了确保你的项目没有得到1.0只是使用''在B的依赖关系 –
nullpointer
当B-1.0运行时,它在运行时无法找到所需的A-1.0的类。运行时有A-1.1类。 – ieXcept
你是什么意思*当B-1.0运行*时?当你将它声明为一个依赖项时,假设它已经使用它自己的pom.xml中提到的依赖关系来构建。 – nullpointer