如何以编程方式列出所有传递依赖项,包括使用DependencyGraphBuilder在Maven中重写的依赖项?

时间:2012-11-02 17:45:21

标签: maven maven-3 aether

这与其他问题(like this)类似,但我希望能够使用最新的API执行此操作。 maven-dependency-plugin:tree verbose选项已被弃用,并且在最新的(2.5.1)代码中没有做任何事情,所以没有很好的例子说明如何做。

1 个答案:

答案 0 :(得分:3)

我相信来自Aetherjcabi-aether实用程序类可以帮助您获取任何Maven工件的所有依赖项的列表,例如:

File repo = this.session.getLocalRepository().getBasedir();
Collection<Artifact> deps = new Aether(this.getProject(), repo).resolve(
  new DefaultArtifact("junit", "junit-dep", "", "jar", "4.10"),
  JavaScopes.RUNTIME
);

如果你不在Maven插件中:

File repo = new File("/tmp/local-repository");
MavenProject project = new MavenProject();
project.setRemoteProjectRepositories(
  Arrays.asList(
    new RemoteRepository(
      "maven-central",
      "default",
      "http://repo1.maven.org/maven2/"
    )
  )
);
Collection<Artifact> deps = new Aether(project, repo).resolve(
  new DefaultArtifact("junit", "junit-dep", "", "jar", "4.10"),
  "runtime"
);

您需要的唯一依赖是:

<dependency>
  <groupId>com.jcabi</groupId>
  <artifactId>jcabi-aether</artifactId>
  <version>0.7.5</version>
</dependency>
相关问题