Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to recursively resolve dependencies in a Maven 2 plugin

I'm writing a Maven 2 plugin which must iterate over all project dependencies and recursively over all dependencies of these dependencies. Up to now I only managed to resolve the direct dependencies with this code:

for (Dependency dependency : this.project.getModel().getDependencies())
{
    Artifact artifact = this.artifactFactory.createArtifact(
        dependency.getGroupId(),
        dependency.getArtifactId(),
        dependency.getVersion(),
        dependency.getScope(),
        dependency.getType());
    this.artifactResolver.resolve(
         artifact,
         this.remoteRepositories,
         this.localRepository);

    ....
}

How can I do the same recursively so I also find the dependencies of the dependencies and so on?

like image 916
kayahr Avatar asked Dec 10 '22 10:12

kayahr


1 Answers

A) Don't use project.getModel().getDependencies(), use project.getArtifacts() instead. That way you automatically get the transitive dependencies. To enable that: Mark your mojo as

  • @requiresDependencyResolution compile or
  • @requiresDependencyCollection compile

(see the Mojo API Specification for reference).

B) Do you really want to use the legacy dependency API? Why not use the new Maven 3 Aether API?

like image 72
Sean Patrick Floyd Avatar answered Dec 28 '22 08:12

Sean Patrick Floyd