2016-06-10 158 views
0

我寫一個庫,具有以下依存關係:從另一個依賴指定的依賴性明確Maven的

```

<dependency> 
    <groupId>io.dropwizard.metrics</groupId> 
    <artifactId>metrics-servlets</artifactId> 
    <version>[3.1.0,)</version> 
</dependency> 
<dependency> 
    <groupId>io.dropwizard.metrics</groupId> 
    <artifactId>metrics-core</artifactId> 
    <version>[3.1.0,)</version> 
</dependency> 
<dependency> 
    <groupId>org.eclipse.jetty</groupId> 
    <artifactId>jetty-servlet</artifactId> 
    <version>9.3.2</version> 
    <scope>provided</scope> 
</dependency> 

```

現在的問題是 - 一些用戶這個庫的使用也與dropwizard-core一起使用,在這種情況下,我的庫爲dropwizard框架提供了一些額外的功能,因此dropwizard-core是一個可選的依賴項。

問題是 - 當用戶進口dropwizard-core - 它會自動將包括我上面指定(如metrics-core),並在這種情況下,其他依賴 - 經由dropwizard-core導入的版本可以與顯式指定的版本衝突。我該如何解決這個問題?

該庫的用戶將使用它 - 沒有dropwizard-core,因此我不希望這些用戶導入dropwizard-core(以及因此原子依賴性)。

+0

誰導入的用戶(我假設你的意思是使用的依賴關係)可以覆蓋你的決定。此外,我建議你永遠不要使用版本範圍....如果你有可選的依賴關係,而不是將它們定義爲可選... – khmarbaise

+0

這是好的,我已經刪除了版本範圍,並制定了特定的版本。但依賴關係仍然是怪異的總體。 –

回答

0

您有兩種選擇。您應該根據使用模式選擇一個。例如,大部分時間排除或包括額外的依賴性?

1.明確排除(默認情況下包括)

保持它,因爲它是,然後要求用戶排除額外如果必要的話:

<dependency> 
    <groupId>some.group.id</groupId> 
    <artifactId>something-important</artifactId> 
    <exclusions> 
    <exclusion> 
     <groupId>some.group.id</groupId> 
     <artifactId>something-extra</artifactId> 
    </exclusion> 
    </exclusions> 
</dependency> 

2.默認排除(包括明確)

修改你的pom.xml:

<dependency> 
    <groupId>some.group.id</groupId> 
    <artifactId>something-extra</artifactId> 
    <optional>true</optional> 
</dependency> 

OR

<dependency> 
    <groupId>some.group.id</groupId> 
    <artifactId>something-extra</artifactId> 
    <scope>provided</scope> 
</dependency> 

使用可選的,當它暗示了你的庫可以工作得很好,沒有它,只是某些部分可能無法正常工作。當暗示您的圖書館需要它時提供使用,但是由用戶決定在哪裏獲取(或實現哪個)。

兩個見文檔: https://maven.apache.org/guides/introduction/introduction-to-optional-and-excludes-dependencies.html

+0

對於你的情況,我會建議使用' true'。 –

+0

現在,我已經選擇使用可選的'something-extra'依賴項。唯一的解決方法是 - 我必須找出與'something-extra'版本兼容的'jetty-servlet'等的確切版本。 –