2

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?

George Z.
  • 6,643
  • 4
  • 27
  • 47
  • In which context do you need this? – J Fabian Meier Dec 06 '20 at 08:24
  • @JFabianMeier I am using a very old AHOT compiler and I compile my jars to `dll`s. But in order the compiler to work, I have to give it the tree of each jar. In order to make `hibernate.dll` I must say to it, to use `javax.persistence` dll. Weird I know. The other thing I could try to do, is to shade all hibernate dependencies to one jar. But I do not know if this is possible too. For each `` declaration, to have only one jar. – George Z. Dec 06 '20 at 13:47

1 Answers1

4

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.
davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • (1+). Unfortunately, this is not enough. I do not want to see them. I know my IDE shows them to me. You can read my comment under my question if you are wondered why I need what I need. – George Z. Dec 06 '20 at 13:48
  • I get it. In your case I see two approaches : 1) scripting the `mvn:dependency-tree` execution since the artifact of your local repository that contains the pom.xml to retrieve. That is what your IDE plugin does in a some way . You can implement it with bash/sh/python or even in java with the `Process` API. 2) The other way is as you recalled : use fat-jar/shading for that hibernate dependency. It should work smoothly if correctly configured. For example Spring Boot relies massively on that and it works nicely. Look at the ``maven-shade-plugin` doc for guideline and examples. – davidxxx Dec 06 '20 at 14:28
  • Configuring `shade` plugin for so many dependencies looks pain. Went with a script. Nevertheless, I was kind of disappointed pure `mvn` is not able to give me this. Thank you for your time. – George Z. Dec 07 '20 at 00:07
  • I added a Maven way. It stays a little cumbersome while looks better. – davidxxx Dec 07 '20 at 12:49
  • Nice one. Thanks. – George Z. Dec 07 '20 at 13:36