2016-12-25 503 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