Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get access to Maven's dependency hierarchy within a plugin

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?

like image 497
talk to frank Avatar asked Sep 29 '09 11:09

talk to frank


People also ask

How do you determine dependency hierarchy?

Eclipse pom. xml “Dependency Hierarchy” tab shows the dependency tree of the project. It has two sides - the left side shows verbose output and the right side shows the resolved dependencies. We can use the “Filter” option to look for a specific dependency.

How do I get the dependency tree in IntelliJ?

In the POM, right-click anywhere in the editor to open the context menu and select Maven | Show Dependencies. Alternatively, press Ctrl+Alt+Shift+U or Ctrl+Alt+U . In the diagram window, IntelliJ IDEA displays the sub project and all its dependencies including the transitive ones.

How do you analyze a dependency tree in Maven?

A project's dependency tree can be filtered to locate specific dependencies. For example, to find out why Velocity is being used by the Maven Dependency Plugin, we can execute the following in the project's directory: mvn dependency:tree -Dincludes=velocity:velocity.

What are plug ins dependency in Maven?

The dependency plugin provides the capability to manipulate artifacts. It can copy and/or unpack artifacts from local or remote repositories to a specified location.


1 Answers

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);          List<DependencyNode> nodes = visitor.getNodes();         for (DependencyNode dependencyNode : nodes) {             int state = dependencyNode.getState();             Artifact artifact = dependencyNode.getArtifact();             if(state == DependencyNode.INCLUDED) {                                     //...             }          }     } catch (DependencyTreeBuilderException e) {         // TODO handle exception         e.printStackTrace();     } } 
like image 156
Rich Seller Avatar answered Sep 25 '22 20:09

Rich Seller