How to get the Maven Dependency Tree of a Project. We can run mvn dependency:tree command in the terminal to print the project dependency tree. For our example, I will be using the Mockito Tutorial project. You can download the project from the GitHub repository.
A dependency tree for a sentence is a directed acyclic graph with words as nodes and relations as edges. Each word in the sentence either modifies another word or is modified by a word. The root of the tree is the only entry that is modified but does not modify anything else.
First go to View → Tool Windows → Maven, to make sure that the Maven window is visible. The top-level elements in the tree are your direct dependencies, and the child elements are the transitive dependencies.
Create a simple project with pom.xml only. Add your dependency and run:
mvn dependency:tree
Unfortunately dependency mojo must use pom.xml or you get following error:
Cannot execute mojo: tree. It requires a project with an existing pom.xml, but the build is not using one.
Dependencies are described In pom.xml of your artifact. Find it using maven infrastructure.
Go to https://search.maven.org/ and enter your groupId and artifactId.
Or you can go to https://repo1.maven.org/maven2/ and navigate first using plugins groupId, later using artifactId and finally using its version.
For example see org.springframework:spring-core
Part of dependency artifact is a pom.xml. That specifies it's dependency. And you can execute mvn dependency:tree on this pom.
If you bother creating a sample project and adding your 3rd party dependency to that, then you can run the following in order to see the full hierarchy of the dependencies.
You can search for a specific artifact using this maven command:
mvn dependency:tree -Dverbose -Dincludes=[groupId]:[artifactId]:[type]:[version]
According to the documentation:
where each pattern segment is optional and supports full and partial * wildcards. An empty pattern segment is treated as an implicit wildcard.
Imagine you are trying to find 'log4j-1.2-api' jar file among different modules of your project:
mvn dependency:tree -Dverbose -Dincludes=org.apache.logging.log4j:log4j-1.2-api
more information can be found here.
Edit: Please note that despite the advantages of using verbose parameter, it might not be so accurate in some conditions. Because it uses Maven 2 algorithm and may give wrong results when used with Maven 3.
The solution is to call dependency:tree with the artifact's pom.xml file:
mvn -f "$HOME/.m2/repository/$POM_PATH" dependency:tree
See also How to list the transitive dependencies of an artifact from a repository?
I know this post is quite old, but still, if anyone using IntelliJ any want to see dependency tree directly in IDE then they can install Maven Helper Plugin plugin.
Once installed open pom.xml and you would able to see Dependency Analyze tab like below. It also provides option to see dependency that is conflicted only and also as a tree structure.
If you use a current version of m2eclipse (which you should if you use eclipse and maven):
Select the menu entry
Navigate -> Open Maven POM
and enter the artifact you are looking for.
The pom will open in the pom editor, from which you can select the tab Dependency Hierarchy
to view the dependency hierarchy (as the name suggests :-) )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With