kayahr
kayahr

Reputation: 22010

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?

Upvotes: 4

Views: 3394

Answers (1)

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 298818

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?

Upvotes: 15

Related Questions