Testing 验证Maven构建中的依赖关系

Testing 验证Maven构建中的依赖关系,testing,maven,dependency-management,Testing,Maven,Dependency Management,有没有办法编写一个检查Maven依赖关系的测试 在我们的项目中,我们发现了以下问题: 项目的某些部分使用commons-io:commons-io,其他部分使用org.apache.commons:commons-io 使用了错误版本的依赖项 stax使用版本1.0-2和1.0.1。使用自动相关性解析,1.0-2wins 因此,我想要编写一个测试用例,它将当前的依赖关系树作为输入,并对其运行一些检查。可能吗 @Test public void testCommonsIo() throws E

有没有办法编写一个检查Maven依赖关系的测试

在我们的项目中,我们发现了以下问题:

  • 项目的某些部分使用
    commons-io:commons-io
    ,其他部分使用
    org.apache.commons:commons-io

  • 使用了错误版本的依赖项

  • stax
    使用版本
    1.0-2
    1.0.1
    。使用自动相关性解析,
    1.0-2
    wins

因此,我想要编写一个测试用例,它将当前的依赖关系树作为输入,并对其运行一些检查。可能吗

@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"));
}
也检查一下


这里是一个用于StAX和其他依赖项的断言方法,这些依赖项不包含
pom.properties
just
manifest.mf
。如果有大量的
assertDependencyByMetaInf
调用,那么缓存
属性
实例可能是值得的

@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);
}
@测试
public void TestStatxDependence()引发异常{
资产依赖性由METAINF(“StAX”、“1.0.1”);
}
私有void assertDependencyByMetaInf(最终字符串,
最终字符串expectedSpecVersion)引发IOException{
final ClassLoader cl=this.getClass().getClassLoader();
最后一个字符串resourcePath=“META-INF/MANIFEST.MF”;
最终枚举资源=cl.getResources(resourcePath);
布尔值=false;
while(resources.hasMoreElements()){
最终URL=resources.nextElement();
final InputStream metaInfStream=url.openStream();
最终属性metaInf=新属性();
加载metaInf.load(metaInfStream);
最终字符串MetainSpecify=
metaInf.getProperty(“规范标题”);
如果(!specifyle.equals(metainspecifyle)){
继续;
}
最终字符串版本=
getProperty(“规范版本”);
assertEquals(“+Specitle”的版本不匹配,
expectedSpecVersion,specVersion);
发现=真;
}
assertTrue(“缺少依赖项:”+Specitle,已找到);
}

根据palacsint的建议,这里有另一种方法:计算类在类路径上的频率,如果有多个URL,则打印URL

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" ) );
    }
}
private void AssertonClassPath(字符串resourcePath)引发IOException{
ClassLoader cl=getClass().getClassLoader();
枚举资源=cl.getResources(resourcePath);
列表URL=新的ArrayList();
while(resources.hasMoreElements()){
URL=resources.nextElement();
添加(url);
}
如果(URL.size()!=1){
失败(“预期正好有1项:\n”+StringUtils.join(URL,“\n”));
}
}

-1我检查的所有jar只包含工件本身的pom.properties文件。没有包含任何依赖项。可传递依赖项也在类路径上,因此
getResourceAsStream
查找它们的
pom.properties
。啊,我明白了。改变了我的投票。注意:Maven Central的一些JAR没有pom.properties条目:-(例如stax api()