How to dependency:tree on a specific artifact?

Viewed 967

Title says it all. Given a dependency, how can I get a tree of its dependencies?

Let's say I want to target org.hibernate:hibernate-core:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.4.24.Final</version>
</dependency>

As a result I want:

[INFO] |  \- org.hibernate:hibernate-core:jar:5.4.24.Final:compile
[INFO] |     +- org.jboss.logging:jboss-logging:jar:3.4.1.Final:compile
[INFO] |     +- javax.persistence:javax.persistence-api:jar:2.2:compile
[INFO] |     +- net.bytebuddy:byte-buddy:jar:1.10.17:compile
[INFO] |     +- antlr:antlr:jar:2.7.7:compile
[INFO] |     +- org.jboss.spec.javax.transaction:jboss-transaction-api_1.2_spec:jar:1.1.1.Final:compile
[INFO] |     +- org.jboss:jandex:jar:2.1.3.Final:compile
[INFO] |     +- com.fasterxml:classmate:jar:1.5.1:compile
[INFO] |     +- javax.activation:javax.activation-api:jar:1.2.0:compile
[INFO] |     +- org.dom4j:dom4j:jar:2.1.3:compile
[INFO] |     \- org.hibernate.common:hibernate-commons-annotations:jar:5.1.2.Final:compile

I tried:

mvn dependency:tree -DgroupId=org.hibernate -DartifactId=hibernate-core -Dversion=5.4.24.Final

But It does not work.

:tree or :list does not matter. All I want is the dependencies of a dependency in my project.

If I mvn dependency:tree -Dincludes=org.hibernate:hibernate-core I get:

[INFO]    \- org.hibernate:hibernate-core:jar:5.4.24.Final:compile

and its dependencies are missing.

There is this online "tool" that does exactly what I want. Is it possible to do it with a mvn command?

1 Answers

What you get is expected.

When using the includes user property such as : -Dincludes=org.hibernate:hibernate-core, the output shows the org.hibernate:hibernate-core dependency along the dependency(ies) that pulled that one.
You want the reverse : displaying the dependencies pulled by org.hibernate:hibernate-core.
To achieve that : you need to execute mvn dependency:tree since the org.hibernate:hibernate-core POM.
So You should go with your shell into your local repository and execute that command. But that is an hassle : IDE Plugins for Maven provided by Eclipse and IntelliJ do that very well.

Example with m2e Eclipse plugin.
On the m2 view of your pom.xml, double click on the dependency that you want to develop.

first

And that is done :
second

A Maven way alternative if suitable would be using the dependency:copy goal by specifying pom as classifier :

 #retrieve and store the hibernate pom 
mvn dependency:copy  -Dartifact=org.hibernate:hibernate-core:5.2.14.final:pom

#see the dependencty tree on the hibernate  pom
mvn -f target/dependency/hibernate-core-5.2.14.final.pom dependency:tree

Two notes :

  • it will work even if the artifact is in your local repository (the artifact is first installed in that case).
  • you can specify the current directory as output directory instead of the default path that is target/dependency with the -DoutputDirectory=. flag.
Related