1

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.

Community
  • 1
  • 1
carlspring
  • 31,231
  • 29
  • 115
  • 197

1 Answers1

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));
}
yegor256
  • 102,010
  • 123
  • 446
  • 597
  • I am looking at your project. Two things: 1) Your `sources` artifact seems to not be packaged properly (I see you're packaging it via the assembly plugin, but the directory structure it's producing for the artifact is such that an IDE cannot recognize the sources as they're not in the root directory, but rather -- in the `${project.artifactId}/src/main/java`); 2) this is a great example and I will study it a bit further. Could you perhaps show me how to get a hold of `Artifact`-s instead of working directly with `File`-s? Thanks! – carlspring May 21 '13 at 10:59
  • Would be great if you submit the first problem as [an issue to github](https://github.com/yegor256/jcabi/issues?state=open), I'll fix it soon. For the second question, see my answer, updated – yegor256 May 21 '13 at 13:05
  • Thanks! I have accepted your answer and awarded you the bounty. One small remark: the above pasted code does not compile and it requires a `File` object, instead of a `String`. It otherwise outlines the proper way of doing things using your library. Perhaps you could polish that up in your example, so that someone doesn't wonder in the future. – carlspring May 23 '13 at 13:57
  • WARNING: the jcabi-aether project appears to be abandoned, (their page was last updated some 8 years ago,) and nothing from it works with modern versions of maven and/or aether. Do not waste your time with it. An answer that works today is https://stackoverflow.com/a/40820480/773113 – Mike Nakis May 06 '22 at 16:46