仅从pom文件中检索依赖项

时间:2017-11-29 21:01:07

标签: java maven

我正在做一些静态挖掘,我希望有一个相关库的列表。

有没有办法以编程方式查找pom.xml文件使用的依赖项?

我不能使用.m2 repo或类似的东西,因为代码没有编译。

(换句话说,我想重新创建" Efective POM" pom.xml文件的标签,如Eclipse中所示)

请注意,读取当前项目的pom文件(测试boottstrap)会在要求时提供一组依赖项,但不会使用外部pom文件。 在代码中:

[...]
Model model = mavenReader.read(new FileReader(pomFile));
model.setPomFile(pomFile);

proj= new MavenProject(model);
proj.setRemoteArtifactRepositories(
Arrays.asList((ArtifactRepository) new MavenArtifactRepository(
        "maven-central",
        "http://repo1.maven.org/maven2/", 
        new DefaultRepositoryLayout(), 
        new ArtifactRepositoryPolicy(),
        new ArtifactRepositoryPolicy())));

List<Dependency> dependencies = proj.getDependencies();
for (Dependency dependency : dependencies) {
    System.out.println(dependency);
}

使用pomFile = new File("pom.xml"),它大叫:

Dependency {groupId=com.github.javaparser, artifactId=javaparser-core, version=3.5.4, type=jar}
Dependency {groupId=com.jcabi, artifactId=jcabi-aether, version=0.7.19, type=jar}
Dependency {groupId=org.apache.maven, artifactId=maven-core, version=3.0.3, type=jar}
Dependency {groupId=org.apache.maven.plugins, artifactId=maven-dependency-plugin, version=3.0.2, type=jar}
Dependency {groupId=org.apache.maven.plugin-tools, artifactId=maven-plugin-annotations, version=3.5, type=jar}

使用XX建议,Runtime.getRuntime().exec("cmd /c mvn dependency:tree");

输出是(在其他人之间):

[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ ca.umontreal.iro.logiannot ---
[INFO] ca.umontreal.iro.logiannot:ca.umontreal.iro.logiannot:jar:0.0.1-SNAPSHOT
[INFO] +- com.github.javaparser:javaparser-core:jar:3.5.4:compile
[INFO] +- com.jcabi:jcabi-aether:jar:0.7.19:compile
[INFO] |  +- com.jcabi:jcabi-aspects:jar:0.7.19:compile
[INFO] |  +- com.jcabi:jcabi-log:jar:0.7.19:compile
[INFO] |  |  +- log4j:log4j:jar:1.2.17:compile
[INFO] |  |  \- org.slf4j:slf4j-api:jar:1.7.5:compile
[INFO] |  +- org.aspectj:aspectjrt:jar:1.7.2:compile
[INFO] |  +- javax.validation:validation-api:jar:1.1.0.Final:compile
[INFO] |  +- org.kuali.maven.wagons:maven-s3-wagon:jar:1.1.20:compile
[INFO] |  |  +- org.kuali.common:kuali-s3:jar:1.0.1:compile
[INFO] |  |  +- com.amazonaws:aws-java-sdk:jar:1.4.2:compile
[INFO] |  |  |  +- org.apache.httpcomponents:httpclient:jar:4.1:compile
[INFO] |  |  |  |  \- org.apache.httpcomponents:httpcore:jar:4.1:compile
[INFO] |  |  |  +- org.codehaus.jackson:jackson-core-asl:jar:1.8.9:compile
[INFO] |  |  |  \- org.codehaus.jackson:jackson-mapper-asl:jar:1.8.9:compile
[INFO] |  |  +- org.springframework:spring-core:jar:3.1.2.RELEASE:compile
[INFO] |  |  +- org.kuali.common:kuali-threads:jar:1.0.9:compile
[INFO] |  |  \- org.slf4j:jcl-over-slf4j:jar:1.6.4:compile
[INFO] |  +- org.sonatype.aether:aether-api:jar:1.13.1:compile
[INFO] |  +- org.sonatype.aether:aether-spi:jar:1.13.1:compile
[INFO] |  +- org.sonatype.aether:aether-util:jar:1.13.1:compile
[INFO] |  +- org.sonatype.aether:aether-connector-file:jar:1.13.1:compile
[INFO] |  +- org.sonatype.aether:aether-connector-asynchttpclient:jar:1.13.1:compile
[INFO] |  |  \- com.ning:async-http-client:jar:1.6.5:compile
[INFO] |  |     \- org.jboss.netty:netty:jar:3.2.5.Final:compile
[INFO] |  +- org.sonatype.aether:aether-connector-wagon:jar:1.13.1:compile
[INFO] |  +- org.sonatype.aether:aether-impl:jar:1.13.1:compile
[INFO] |  +- org.apache.maven:maven-aether-provider:jar:3.0.5:compile
[INFO] |  +- org.apache.maven.wagon:wagon-provider-api:jar:2.4:compile
[INFO] |  \- org.apache.commons:commons-lang3:jar:3.1:compile
[INFO] +- org.apache.maven:maven-core:jar:3.0.3:compile
[INFO] |  +- org.apache.maven:maven-model:jar:3.0.3:compile
[INFO] |  +- org.apache.maven:maven-settings:jar:3.0.3:compile
[INFO] |  +- org.apache.maven:maven-settings-builder:jar:3.0.3:compile
[INFO] |  +- org.apache.maven:maven-repository-metadata:jar:3.0.3:compile
[INFO] |  +- org.apache.maven:maven-artifact:jar:3.0.3:compile
[INFO] |  +- org.apache.maven:maven-plugin-api:jar:3.0.3:compile
[INFO] |  +- org.apache.maven:maven-model-builder:jar:3.0.3:compile
[INFO] |  +- org.sonatype.sisu:sisu-inject-plexus:jar:2.1.1:compile
[INFO] |  |  \- org.sonatype.sisu:sisu-inject-bean:jar:2.1.1:compile
[INFO] |  |     \- org.sonatype.sisu:sisu-guice:jar:no_aop:2.9.4:compile
[INFO] |  +- org.codehaus.plexus:plexus-interpolation:jar:1.14:compile
[INFO] |  +- org.codehaus.plexus:plexus-utils:jar:2.0.6:compile
[INFO] |  +- org.codehaus.plexus:plexus-classworlds:jar:2.4:compile
[INFO] |  +- org.codehaus.plexus:plexus-component-annotations:jar:1.5.5:compile
[INFO] |  \- org.sonatype.plexus:plexus-sec-dispatcher:jar:1.3:compile
[INFO] |     \- org.sonatype.plexus:plexus-cipher:jar:1.4:compile
[INFO] +- org.apache.maven.plugins:maven-dependency-plugin:jar:3.0.2:compile
[INFO] |  +- org.apache.maven.reporting:maven-reporting-api:jar:3.0:compile
[INFO] |  +- org.apache.maven.reporting:maven-reporting-impl:jar:2.3:compile
[INFO] |  |  +- org.apache.maven.doxia:doxia-core:jar:1.2:compile
[INFO] |  |  |  \- xerces:xercesImpl:jar:2.9.1:compile
[INFO] |  |  |     \- xml-apis:xml-apis:jar:1.3.04:compile
[INFO] |  |  \- commons-validator:commons-validator:jar:1.3.1:compile
[INFO] |  |     +- commons-beanutils:commons-beanutils:jar:1.7.0:compile
[INFO] |  |     +- commons-digester:commons-digester:jar:1.6:compile
[INFO] |  |     \- commons-logging:commons-logging:jar:1.0.4:compile
[INFO] |  +- commons-io:commons-io:jar:2.5:compile
[INFO] |  +- org.apache.maven.doxia:doxia-sink-api:jar:1.4:compile
[INFO] |  |  \- org.apache.maven.doxia:doxia-logging-api:jar:1.4:compile
[INFO] |  +- org.apache.maven.doxia:doxia-site-renderer:jar:1.4:compile
[INFO] |  |  +- org.apache.maven.doxia:doxia-decoration-model:jar:1.4:compile
[INFO] |  |  +- org.apache.maven.doxia:doxia-module-xhtml:jar:1.4:compile
[INFO] |  |  +- org.apache.maven.doxia:doxia-module-fml:jar:1.4:compile
[INFO] |  |  +- org.codehaus.plexus:plexus-i18n:jar:1.0-beta-7:compile
[INFO] |  |  +- org.codehaus.plexus:plexus-container-default:jar:1.0-alpha-30:compile
[INFO] |  |  |  \- junit:junit:jar:3.8.1:compile
[INFO] |  |  +- org.codehaus.plexus:plexus-velocity:jar:1.1.7:compile
[INFO] |  |  +- org.apache.velocity:velocity:jar:1.5:compile
[INFO] |  |  |  \- oro:oro:jar:2.0.8:compile
[INFO] |  |  \- org.apache.velocity:velocity-tools:jar:2.0:compile
[INFO] |  |     +- commons-chain:commons-chain:jar:1.1:compile
[INFO] |  |     +- dom4j:dom4j:jar:1.1:compile
[INFO] |  |     +- sslext:sslext:jar:1.2-0:compile
[INFO] |  |     +- org.apache.struts:struts-core:jar:1.3.8:compile
[INFO] |  |     |  \- antlr:antlr:jar:2.7.2:compile
[INFO] |  |     +- org.apache.struts:struts-taglib:jar:1.3.8:compile
[INFO] |  |     \- org.apache.struts:struts-tiles:jar:1.3.8:compile
[INFO] |  +- org.codehaus.plexus:plexus-archiver:jar:3.4:compile
[INFO] |  |  +- org.apache.commons:commons-compress:jar:1.11:compile
[INFO] |  |  +- org.iq80.snappy:snappy:jar:0.4:compile
[INFO] |  |  \- org.tukaani:xz:jar:1.5:runtime
[INFO] |  +- org.apache.maven.shared:file-management:jar:1.2.1:compile
[INFO] |  |  \- org.apache.maven.shared:maven-shared-io:jar:1.1:compile
[INFO] |  +- org.codehaus.plexus:plexus-io:jar:2.4:compile
[INFO] |  +- org.apache.maven.shared:maven-dependency-analyzer:jar:1.7:compile
[INFO] |  |  \- org.ow2.asm:asm:jar:5.0.2:compile
[INFO] |  +- org.apache.maven.shared:maven-dependency-tree:jar:3.0.1:compile
[INFO] |  |  \- org.eclipse.aether:aether-util:jar:0.9.0.M2:compile
[INFO] |  +- org.apache.maven.shared:maven-common-artifact-filters:jar:3.0.1:compile
[INFO] |  +- org.apache.maven.shared:maven-artifact-transfer:jar:0.9.1:compile
[INFO] |  |  \- commons-codec:commons-codec:jar:1.6:compile
[INFO] |  +- org.apache.maven.shared:maven-shared-utils:jar:3.2.0:compile
[INFO] |  +- commons-lang:commons-lang:jar:2.6:compile
[INFO] |  +- commons-collections:commons-collections:jar:3.2.2:compile
[INFO] |  \- classworlds:classworlds:jar:1.1:compile
[INFO] \- org.apache.maven.plugin-tools:maven-plugin-annotations:jar:3.5:provided

完全正是我想要的。但问题是,我不想知道我的项目的依赖树是什么样的。我想调查其他项目。

现在,使用pomFile = new File("[externalPath].pom.xml"),它找不到任何依赖项。 (该文件存在且包含正确的pom isntructions)。

1 个答案:

答案 0 :(得分:1)

您可以启动一个运行mvn help:effective-pom的单独进程。此命令返回带有“有效pom”的xml。稍后您可以解析该XML以获取依赖关系。

如果您对传递依赖感兴趣,可以以类似的方式使用mvn dependency:tree