How to get a dependency tree for an artifact?

Maven 2Build ProcessDependencies

Maven 2 Problem Overview


dependency:tree can be used to see the dependency tree for a given project. But what I need is to see the dependency tree for a 3rd party artifact.

I guess I can create an empty project, but I'm looking for something easier (I need to do this for several artifacts).

Maven 2 Solutions


Solution 1 - Maven 2

1) Use maven dependency plugin

Create a simple project with pom.xml only. Add your dependency and run:

mvn dependency:tree

(Version for multi-module Maven project: mvn compile 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.

2) Find pom.xml of your artifact in maven central repository

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

3) Use maven dependency plugin against your artifact

Part of dependency artifact is a pom.xml. That specifies it's dependency. And you can execute mvn dependency:tree on this pom.

Solution 2 - Maven 2

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: The verbose flag has been reintroduced in version 3.2.0. You can use the specific version as below:

mvn org.apache.maven.plugins:maven-dependency-plugin:3.2.0:tree -Dverbose=true 

Solution 3 - Maven 2

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 https://stackoverflow.com/questions/22355688/how-to-list-the-transitive-dependencies-of-an-artifact-from-a-repository

Solution 4 - Maven 2

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.

enter image description here

Solution 5 - Maven 2

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 :-) )

Solution 6 - Maven 2

I created an online tool to do this. Simply paste any dependency in pom file format, and the dependency tree for that artifact is generated, based on the central maven repository.

Solution 7 - Maven 2

When using IntelliJ, you have a one-click tool for Maven dependency exploration: https://www.jetbrains.com/help/idea/work-with-maven-dependencies.html

Results: enter image description here

Solution 8 - Maven 2

If your artifact is not a dependency of a given project, your best bet is to use a repository search engine. Many of them describes the dependencies of a given artifact.

Solution 9 - Maven 2

The for-artifact goal of the depgraph-maven-plugin should do what you want. To get a dependency graph of org.jboss.ws:jbossws-common-tools:1.3.2.Final you simply do

mvn com.github.ferstl:depgraph-maven-plugin:3.3.0:for-artifact -DgroupId=org.jboss.ws -DartifactId=jbossws-common-tools -Dversion=1.3.2.Final -DgraphFormat=text -DshowGroupIds=true -DshowVersions=true

and you get

org.jboss.ws:jbossws-common-tools:1.3.2.Final:compile
+- org.jboss.ws:jbossws-api:1.1.2.Final:compile
+- org.apache.ant:ant:1.7.1:provided
|  \- org.apache.ant:ant-launcher:1.7.1:provided
+- gnu.getopt:java-getopt:1.0.13:provided
+- log4j:log4j:1.2.14:provided
\- junit:junit:3.8.2:test

without the need to create a Maven project or use and IDE or online app.

Solution 10 - Maven 2

If you'd like to get a graphical, searchable representation of the dependency tree (including all modules from your project, transitive dependencies and eviction information), check out UpdateImpact: https://app.updateimpact.com (free service).

Disclaimer: I'm one of the developers of the site

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionIttayDView Question on Stackoverflow
Solution 1 - Maven 2amraView Answer on Stackoverflow
Solution 2 - Maven 2Kayvan TehraniView Answer on Stackoverflow
Solution 3 - Maven 2Matthias MView Answer on Stackoverflow
Solution 4 - Maven 2rnsView Answer on Stackoverflow
Solution 5 - Maven 2Sean Patrick FloydView Answer on Stackoverflow
Solution 6 - Maven 2Philip DodsonView Answer on Stackoverflow
Solution 7 - Maven 2VaidenView Answer on Stackoverflow
Solution 8 - Maven 2Pascal ThiventView Answer on Stackoverflow
Solution 9 - Maven 2Philippe MarschallView Answer on Stackoverflow
Solution 10 - Maven 2adamwView Answer on Stackoverflow