有沒有辦法編寫一個檢查Maven依賴關係的測試?驗證Maven構建中的依賴關係
在我們的項目中,我們發現了以下問題:
項目的某些部分使用
commons-io:commons-io
,他人使用org.apache.commons:commons-io
依賴的錯誤版本,使用
stax
使用版本1.0-2
和1.0.1
。使用自動依賴關係解析,1.0-2
獲勝。
所以,我想要的是編寫測試用例這需要當前依賴關係樹作爲輸入,並運行一對夫婦檢查就可以了。那可能嗎?
有沒有辦法編寫一個檢查Maven依賴關係的測試?驗證Maven構建中的依賴關係
在我們的項目中,我們發現了以下問題:
項目的某些部分使用commons-io:commons-io
,他人使用org.apache.commons:commons-io
依賴的錯誤版本,使用
stax
使用版本1.0-2
和1.0.1
。使用自動依賴關係解析,1.0-2
獲勝。
所以,我想要的是編寫測試用例這需要當前依賴關係樹作爲輸入,並運行一對夫婦檢查就可以了。那可能嗎?
@Test
public void testCommonsIo() throws Exception {
assertDependencyVersion("commons-io", "commons-io", "2.0");
}
private void assertDependencyVersion(final String groupId,
final String artifactId, final String expectedVersion)
throws IOException {
final StringBuilder sb = new StringBuilder("/META-INF/maven/");
sb.append(groupId).append("/").append(artifactId);
sb.append("/pom.properties");
final String resourcePath = sb.toString();
final InputStream propertiesStream = this.getClass()
.getResourceAsStream(resourcePath);
assertNotNull("no dependency found: " + groupId + ":" + artifactId,
propertiesStream);
final Properties properties = new Properties();
properties.load(propertiesStream);
assertEquals("group", groupId, properties.getProperty("groupId"));
assertEquals("artifact", artifactId, properties.getProperty("artifactId"));
assertEquals("version", expectedVersion, properties.getProperty("version"));
}
也檢查this answer。
這裏是StAX的和其他依賴斷言方法不包含pom.properties
只是manifest.mf
。如果有很多assertDependencyByMetaInf
調用,也許值得緩存Properties
實例。
@Test
public void testStaxDependency() throws Exception {
assertDependencyByMetaInf("StAX", "1.0.1");
}
private void assertDependencyByMetaInf(final String specTitle,
final String expectedSpecVersion) throws IOException {
final ClassLoader cl = this.getClass().getClassLoader();
final String resourcePath = "META-INF/MANIFEST.MF";
final Enumeration<URL> resources = cl.getResources(resourcePath);
boolean found = false;
while (resources.hasMoreElements()) {
final URL url = resources.nextElement();
final InputStream metaInfStream = url.openStream();
final Properties metaInf = new Properties();
metaInf.load(metaInfStream);
final String metaInfSpecTitle =
metaInf.getProperty("Specification-Title");
if (!specTitle.equals(metaInfSpecTitle)) {
continue;
}
final String specVersion =
metaInf.getProperty("Specification-Version");
assertEquals("version mismatch for " + specTitle,
expectedSpecVersion, specVersion);
found = true;
}
assertTrue("missing dependency: " + specTitle, found);
}
繼從palacsint的建議,這裏是另一種方法:計算一類是在類路徑上的頻率和打印網址,如果有不止一個。
private void assertOnceOnClassPath(String resourcePath) throws IOException {
ClassLoader cl = getClass().getClassLoader();
Enumeration<URL> resources = cl.getResources(resourcePath);
List<URL> urls = new ArrayList<URL>();
while(resources.hasMoreElements()) {
URL url = resources.nextElement();
urls.add(url);
}
if(urls.size() != 1) {
fail("Expected exactly 1 item:\n" + StringUtils.join(urls, "\n"));
}
}
-1,我只檢查了所有的JAR文件包含神器本身pom.properties文件。沒有包括依賴關係。 –
傳遞依賴也在類路徑上,所以'getResourceAsStream'找到他們的'pom.properties'。 – palacsint
啊,我明白了。改變了我的投票。注意:來自Maven Central的一些JAR沒有pom.properties條目:-(例如stax-api(http://search.maven.org/#artifactdetails%7Cstax%7Cstax-api%7C1.0.1%7Cjar) –