2009-02-13 105 views
175

在Maven2的,排除單個傳遞依賴的所有傳遞依賴,我必須做這樣的事情:排除單個依賴

<dependency> 
    <groupId>sample.group</groupId> 
    <artifactId>sample-artifactB</artifactId> 
    <version>1</version> 
    <exclusions> 
    <exclusion> 
     <groupId>sample.group</groupId> 
     <artifactId>sample-artifactAB</artifactId> 
    </exclusion> 
    </exclusions> 
</dependency> 

這種方法的問題是,我必須爲每一個做到這一點傳遞依賴由sample-artifactB貢獻。

有沒有辦法使用某種通配符來一次排除所有傳遞依賴關係而不是一個接一個?

+0

有時我們需要使用Spring 2.5.6的最新版本的庫,但其他一些依賴包括舊版本,例如struts2-spring-plugin(2.1.6)包含Spring 2.5.3。在這種情況下,需要排除或覆蓋版本。 – 2009-04-28 20:49:05

+0

使用常春藤。開玩笑。 – 2014-05-07 21:59:22

回答

38

對於maven2,沒有辦法做你所描述的。對於maven 3,有。如果您使用的是maven 3,請參閱another answer for this question

對於maven 2,我建議您爲依賴關係創建自己的自定義pom,其中包含<排除項>。對於需要使用該依賴項目的項目,請將依賴項設置爲您的自定義pom,而不是典型的工件。雖然這並不一定允許您排除所有傳遞依賴關係,排除<排除>,但它確實允許您只需編寫一次依賴項,而且所有項目都不需要維護不必要的長排除列表。

+12

我建議不要讓自己的pom解決排除問題。這使得你的構建更加便攜,並減少了理解。 – 2009-04-12 01:52:14

+1

如果你不看過去接受的anwser:http://jira.codehaus.org/browse/MNG-3832 – 2015-03-17 20:39:45

+0

@JakubBochenski我給出的答案是特定於maven 2,這是這個問題的標籤是什麼(當時我寫這個評論)。您的鏈接僅適用於maven 3.無論如何,我已經編輯了我的答案,鏈接到更高級的答案。 – whaley 2015-03-17 23:50:47

3

排除所有傳遞依賴的原因是什麼?

如果您需要從每個依賴項中排除特定工件(例如commons-logging),則Version 99 Does Not Exist方法可能會有所幫助。


更新2012:不要使用這種方法。使用maven-enforcer-plugin and exclusions。版本99會產生僞造依賴關係,並且版本99存儲庫處於脫機狀態(有similar mirrors,但您不能依賴它們永遠保持聯機狀態;最好僅使用Maven Central)。

-3

您可以使用排除,但你必須手動列出需要排除的每一個依賴...

<dependency> 
    <groupId>sample.group</groupId> 
    <artifactId>sample-artifactB</artifactId> 
    <version>1</version> 
    <exclusions> 
    <exclusion> 
     <groupId>sample.group</groupId> 
     <artifactId>sample-artifactAB</artifactId> 
    </exclusion> 
    </exclusions> 
</dependency> 
+13

幹得好,你已經成功地重複了這個問題。 – thecoshman 2013-12-05 11:19:10

6

有解決類似的問題,如果你依賴的範圍設定爲運行,及物DEP將被排除在外。雖然請注意,這意味着如果要打包運行時依賴關係,則需要添加其他處理。

要在任何打包中包含運行時依賴項,您可以使用maven-dependency-plugin的copy goal for a specific artifact

29

一兩件事,我發現有用:

如果你把在任何您的項目父POM的dependencyManagement部分排除的依賴,或在導入的依賴管理POM,那麼你不需要重複排除(或版本)。

例如,如果你的父母POM具有:

<dependencyManagement> 
    <dependencies> 
    ...   
     <dependency> 
      <groupId>commons-fileupload</groupId> 
      <artifactId>commons-fileupload</artifactId> 
      <version>1.2.1</version> 
      <exclusions> 
       <exclusion> 
        <groupId>junit</groupId> 
        <artifactId>junit</artifactId> 
       </exclusion> 
      </exclusions> 
     </dependency> 
    .... 
    </dependencies> 
</dependencyManagement> 

然後在你的項目中的模塊可以簡單地聲明依賴爲:在父

 <dependency> 
      <groupId>commons-fileupload</groupId> 
      <artifactId>commons-fileupload</artifactId> 
     </dependency> 

的POM將同時指定版本和排除。我幾乎將所有這些技術用於我們的項目,並消除了很多重複。

219

對我有用(可能是Maven的一個新特性)只是在排除元素中做通配符。

我有一個多模塊項目,其中包含兩個WAR打包模塊中引用的「應用程序」模塊。其中一個WAR打包的模塊實際上只需要域類(我還沒有將它們從應用程序模塊中分離出來)。我發現這工作:

<dependency> 
    <groupId>${project.groupId}</groupId> 
    <artifactId>app</artifactId> 
    <version>${project.version}</version> 
    <exclusions> 
     <exclusion> 
      <groupId>*</groupId> 
      <artifactId>*</artifactId> 
     </exclusion> 
    </exclusions> 
</dependency> 

兩個groupId和artifactId的通配符排除,通常會通過傳播到使用這種依賴的模塊的所有依賴關係。

3

如果你的Eclipse下開發,就可以在POM編輯器(啓用了高級選項卡)爲您要排除的項目,然後的依賴性依賴圖看:

右擊它 - >「排除的Maven Artifact ...「,並且Eclipse將爲您排除這些內容,而無需查明lib鏈接的依賴關係。

1

在一個類似的問題中,我有所需的依賴聲明與提供的範圍聲明。 通過這種方法,可以獲取傳遞依賴關係,但不包含在包階段,這正是您想要的。 我也喜歡這個解決方案的維護方面,因爲在whaley的解決方案中沒有pom或自定義pom需要維護;您只需要在容器中提供特定的依賴關係並執行

6

如果您需要從要包括在程序集中的依賴項工件中排除所有傳遞性依賴項,則可以在該程序集的描述符中指定此屬性-plugin:

<assembly> 
    <id>myApp</id> 
    <formats> 
     <format>zip</format> 
    </formats> 
    <dependencySets> 
     <dependencySet> 
      <useTransitiveDependencies>false</useTransitiveDependencies> 
      <includes><include>*:struts2-spring-plugin:jar:2.1.6</include></includes> 
     </dependencySet> 
    </dependencySets> 
</assembly> 
20

Three years ago我建議使用版本99不存在,但現在我已經想出一個更好的辦法,特別是因爲版本99離線:

在項目的父POM,使用maven-enforcer-plugin如果不必要的依賴陷入,就會失敗構建。這可以通過使用插件的banned dependencies規則來執行:

<plugin> 
    <artifactId>maven-enforcer-plugin</artifactId> 
    <version>1.0.1</version> 
    <executions> 
     <execution> 
      <id>only-junit-dep-is-used</id> 
      <goals> 
       <goal>enforce</goal> 
      </goals> 
      <configuration> 
       <rules> 
        <bannedDependencies> 
         <excludes> 
          <exclude>junit:junit</exclude> 
         </excludes> 
        </bannedDependencies> 
       </rules> 
      </configuration> 
     </execution> 
    </executions> 
</plugin> 

然後當,提醒你有關的不必要的依賴,排除它在父POM的<dependencyManagement>部分:

<dependency> 
    <groupId>org.springframework.batch</groupId> 
    <artifactId>spring-batch-test</artifactId> 
    <version>2.1.8.RELEASE</version> 
    <exclusions> 
     <exclusion> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
     </exclusion> 
    </exclusions> 
</dependency> 

這樣,不需要依賴贏得」 (與<exclusion>不同,它很容易被忽略),即使在編譯期間它也不可用(與provided作用域不同),不存在假依賴(不像99版本),它不需要定製存儲庫(與版本99不同)。這種方法甚至可以基於工件的版本,分類器,範圍或整個groupId - see the documentation獲取詳細信息。

7

我使用下面的解決方法:而不是試圖排除在所有適當的依賴關係中的工件,我將依賴關係作爲「提供」在頂層繪製。 例如,爲了避免航運XML的API「任何版本」:

<dependency> 
     <groupId>xml-apis</groupId> 
     <artifactId>xml-apis</artifactId> 
     <version>[1.0,]</version> 
     <scope>provided</scope> 
    </dependency> 
0

使用在類路徑中最新的行家。它會刪除重複的文物和保持最新的Maven構件..