如果你的依賴是在依賴關係樹同級別,申報在你的pom的訂貨會勝利,因爲每Maven Dependency Mediation DOC:
注意,如果兩個依賴的版本是在同一深度依賴關係樹,直到Maven 2.0.8它沒有定義哪一個會贏,但自從Maven 2.0.9它是在聲明中的順序計數:第一個聲明贏得
所以在你的情況下,兩個依賴在依賴關係樹(第一級,在POM中聲明)和處於同一級別範圍將(根據您發佈的剪斷)的方式存在,只要test
範圍會,因爲這樣jar2
會贏,因爲你在你的POM宣佈它第一。
如果您希望文件始終從jar3
加載,但僅在測試期間,只需在您的依賴關係中首先聲明jar3
即可。它不會影響最終的可交付成果(在test
作用域中),但它將定義到類路徑的測試順序,併爲您提供預期的方案。你不需要Maven配置文件。
一個簡單的測試用例來驗證:
讓我們來定義一個屬性file.properties
到Maven項目的src\main\resources
。該文件看起來像項目resource-provider
(的artifactId)以下:
property=from-resource-provider
而且如下項目resource-provider2
(的artifactId):
property=from-resource-provider2
注:相同的文件名與不同的兩個不同的項目內容。
然後在消費項目(resource-consumer
),我們可以有以下示例JUnit測試案例:
public class MainTest {
@Test
public void checkClassPath() {
InputStream is = MainTest.class.getResourceAsStream("/file.properties");
Scanner s = new Scanner(is);
System.out.println(s.nextLine());
}
}
對於下面的依賴關係到resource-consumer
:
<dependencies>
<dependency>
<groupId>com.sample</groupId>
<artifactId>resource-provider2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sample</groupId>
<artifactId>resource-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
測試執行的輸出將是:
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.sample.MainTest
property=from-resource-provider2
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.051 sec
因此,第一個de申報失敗,resource-provider2
,贏了(注意範圍,test
)。
改變依賴以:
<dependencies>
<dependency>
<groupId>com.sample</groupId>
<artifactId>resource-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.sample</groupId>
<artifactId>resource-provider2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
反而會提供以下的輸出:
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.sample.MainTest
property=from-resource-provider
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.07 sec
注意:這個時候resource-provider
韓元,因爲它被宣佈第一,因爲compile
範圍也部分test
的範圍,而相反的情況並非如此。
..謝謝你..很明確的解釋.. –
@svsteja不客氣。附註:當使用這種方法時(基於訂購),我會建議添加評論給你的POM,以幫助其他開發人員或未來的維護和故障排除:) –