views:

291

answers:

2

In my plugin I need to process the dependency hierarchy and get information (groupId, artifactId, version etc) about each dependency and if it was excluded. What is the best way to do this?

+4  A: 

The dependency plugin has the tree goal that does most of this work. It processes a MavenProject using the DependencyTreeBuilder, this returns a DependencyNode with hierarchical information about the resolved dependencies (and their transitive dependencies).

You can copy much of the code directly from the TreeMojo. It uses the CollectingDependencyNodeVisitor to traverse the tree and produce a List of all the nodes.

You can access the Artifact for the node by calling getArtifact(), then get the artifact information as needed. To get the exclusion reason, DependencyNode has a getState() method that returns an int indicating if the dependency has been included, or if not what the reason for omitting it was (there are constants in the DependencyNode class to check the return value against)

//All components need this annotation, omitted for brevity

/**
 * @component
 * @required
 * @readonly
 */
private ArtifactFactory artifactFactory;
private ArtifactMetadataSource artifactMetadataSource;
private ArtifactCollector artifactCollector;
private DependencyTreeBuilder treeBuilder;
private ArtifactRepository localRepository;
private MavenProject project;

public void execute() throws MojoExecutionException, MojoFailureException {
    try {
        ArtifactFilter artifactFilter = new ScopeArtifactFilter(null);

        DependencyNode rootNode = treeBuilder.buildDependencyTree(project,
                localRepository, artifactFactory, artifactMetadataSource,
                artifactFilter, artifactCollector);

        CollectingDependencyNodeVisitor visitor = 
            new CollectingDependencyNodeVisitor();

        rootNode.accept(visitor);

        for (DependencyNode dependencyNode : nodes) {
            int state = dependencyNode.getState();
            Artifact artifact = dependencyNode.getArtifact();
            if(state == DependencyNode.INCLUDED) {                    
                //...
            } 
        }
    } catch (DependencyTreeBuilderException e) {
        // TODO handle exception
        e.printStackTrace();
    }
}
Rich Seller
+1 Very nice, thanks! However, the snippet contains a small error:List< DependencyNode > nodes = visitor.getNodes();Above the for loop.
javamonkey79
+3  A: 

You could use MavenProject#getDependencyArtifacts() or MavenProject#getDependencies() (the later one returns also transitive dependencies).

/**
 * Test Mojo
 *
 * @goal test
 * @requiresDependancyResolution compile
 */
public class TestMojo extends AbstractMojo {

    /**
     * The Maven Project.
     *
     * @parameter expression="${project}"
     * @required
     * @readonly
     */
    private MavenProject project = null;

    /**
     * Execute Mojo.
     *
     * @throws MojoExecutionException If an error occurs.
     * @throws MojoFailureException If an error occurs.
     */
    public void execute() throws MojoExecutionException,
MojoFailureException {

        ...

        Set dependencies = project.getDependencies();

       ...
    }

}

I'm not totally sure but I think both methods return a collection of Artifact implementations that expose getters for groupId, artifactId, version, etc.

Pascal Thivent
+1 This is a simpler solution than mine if you need to get all *resolved* dependencies, but if you want to find out information about excluded dependencies you need more than this
Rich Seller
Damn, you are right, I missed the point about excluded dependencies. So this doesn't answer the OP question.
Pascal Thivent
yes I'm after all dependencies so I can report why they weren't included, thanks for the information though
talk to frank