2016-12-25 509 views
0

TL; DR如何停止在Maven中覆盖传递依赖的版本?

我有一个依赖管理问题。我想简化我的问题的说明,所以我没有要发布的NoClassDefFoundError的全堆栈跟踪:.. VeryImportantStuff等

简单地说,我用2依赖性:A-1.1B- 1.0我的项目C。我不能接受AB的另一个版本。 B-1.0取决于A-1.0A的作者不尊重后向兼容性规则,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

+0

如果* ... B-1.0依赖于A-1.0 *然后B-1.0是建立使用A的1.0只,为了确保你的项目没有得到1.0只是使用''在B的依赖关系 – nullpointer

+0

当B-1.0运行时,它在运行时无法找到所需的A-1.0的类。运行时有A-1.1类。 – ieXcept

+0

你是什么意思*当B-1.0运行*时?当你将它声明为一个依赖项时,假设它已经使用它自己的pom.xml中提到的依赖关系来构建。 – nullpointer

回答

0

只需向依赖项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> 
+0

没有成功。在运行时B中仍然使用A的一个 – ieXcept

+0

1.1所以,你想在单个JVM中同时使用'A'的版本1.0和1.1。如果是的话,那是不可能的。如果你需要在运行时使用同一个类的两个版本,那你只有osgi。 – ares