简体   繁体   English

使用Aether获取MavenProject的所有依赖项(包括可传递的依赖项)

[英]Get all the dependencies of a MavenProject (including transitive ones) using Aether

How can you get all the dependencies of a MavenProject (including transitive ones) using Aether? 如何使用Aether获得MavenProject的所有依赖项(包括可传递的依赖项)?

I have seen numerous examples where you specify the gav and it resolves the artifact and all it's dependencies. 我看到了许多示例,在这些示例中指定了gav并可以解析工件和所有依赖项。 This is all fine. 一切都很好。 However, if your plugin is supposed to be invoked from the same project whose dependencies you're trying to resolve, this does not seem to work (or perhaps I am doing it wrong). 但是,如果应该从要解析其依赖项的同一项目中调用您的插件,则这似乎不起作用(或者我做错了)。 Could somebody please give me a working example of how to do it? 有人可以给我一个如何做的工作例子吗?

I have tried the example with jcabi-aether shown in this SO post . 我已经在本SO post中使用jcabi-aether尝试了该示例。

Try to use an utility class Classpath from jcabi-aether : 尝试从jcabi-aether使用实用程序类Classpath

Collection<File> jars = new Classpath(
  this.getProject(),
  new File(this.session.getLocalRepository().getBasedir()),
  "test" // the scope you're interested in
);

You will get a list of JARs and directories which are in "test" scope in the current Maven project your plugin is in. 您将获得插件所在的当前Maven项目中处于“测试”范围内的JAR和目录的列表。

If you're interested to get a list of Artifact s instead of File s, use Aether class directly: 如果您有兴趣获取Artifact的列表而不是File ,请直接使用Aether类:

Aether aether = new Aether(this.getProject(), repo);
Set<Artifact> artifacts = new HashSet<Artifact>();
for (Artifact dep : this.getProject().getDependencyArtifacts()) {
  artifacts.addAll(aether.resolve(dep, JavaScopes.COMPILE));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM