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

末鹿安然 提交于 2019-12-05 07:13:55

问题


How can you get all the dependencies of a MavenProject (including transitive ones) using Aether?

I have seen numerous examples where you specify the gav and it resolves the artifact and all it's dependencies. 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.


回答1:


Try to use an utility class Classpath from jcabi-aether:

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.

If you're interested to get a list of Artifacts instead of Files, use Aether class directly:

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));
}


来源:https://stackoverflow.com/questions/16480314/get-all-the-dependencies-of-a-mavenproject-including-transitive-ones-using-aet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!