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

StackOverflow https://stackoverflow.com/questions/16480314

  •  21-04-2022
  •  | 
  •  

Вопрос

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.

Это было полезно?

Решение

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));
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top