Java

MVN Dependency Tree

“Dependencies are external codes and snippets imported into your project to provide extra functionality. For example, a dependency could be another archive or file in Java Projects. Other parts of the project then reference the dependency to run.

Having a clear image of the dependency tree for your project can be beneficial. It allows rapidly finding conflicts between installed packages and resolving them efficiently.

In this tutorial, we will learn how to view the dependency tree of a Maven project.”

Let’s get started.

Maven Dependency Plugin

The Apache Maven utility comes with a tool that allows you to manage your dependencies. The Maven Dependency Plugin will enable you to run a simple command in your project and visualize all its dependencies.

The plugin resource is shown below:

https://maven.apache.org/plugins/maven-dependency-plugin/usage.html

The best way to visualize your project dependency tree with this plugin is by running the following command:

mvn dependency:tree

The previous command will locate all the dependencies in your project and return a tree-like structure.

Let us illustrate how we can accomplish this.

You can use any Maven project with any required dependencies to do this.

Once you run the command above, you should see the following example output:

[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ jedis ---
[INFO] redis.clients:jedis:jar:4.3.0-SNAPSHOT
[INFO] +- org.slf4j:slf4j-api:jar:1.7.32:compile
[INFO] +- org.apache.commons:commons-pool2:jar:2.11.1:compile
[INFO] +- org.json:json:jar:20211205:compile
[INFO] +- com.google.code.gson:gson:jar:2.8.9:compile
[INFO] +- junit:junit:jar:4.13.2:test
[INFO] |  \- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] +- org.slf4j:slf4j-simple:jar:1.7.32:test
[INFO] +- com.kohlschutter.junixsocket:junixsocket-core:pom:2.4.0:test
[INFO] |  +- com.kohlschutter.junixsocket:junixsocket-native-common:jar:2.4.0:test
[INFO] |  \- com.kohlschutter.junixsocket:junixsocket-common:jar:2.4.0:test
[INFO] \- org.mockito:mockito-inline:jar:3.12.4:test
[INFO]    \- org.mockito:mockito-core:jar:3.12.4:test
[INFO]       +- net.bytebuddy:byte-buddy:jar:1.11.13:test
[INFO]       +- net.bytebuddy:byte-buddy-agent:jar:1.11.13:test
[INFO]       \- org.objenesis:objenesis:jar:3.2:test

As you can see from the output above, Maven returns all our project’s dependencies in a list format.

It is good to keep in mind that this command requires you to have Maven and Java JDK installed on your system.

Filtering Dependencies

If you are working on a large project, you may find it difficult to view and manage all the dependencies using the above command.

Luckily, the Maven dependency tree plugin allows you to filter for the shown dependencies. This means you can include or exclude any dependency you wish.

To include only a specific dependency, we use the Dincludes option as shown in the following syntax:

mvn dependency:tree -Dincludes [groupId]:[artifactId]:[type]:[version]

Keep in mind that each of the segments in the -Dincludes parameter is optional.

For example, to show how a specific dependency is used in the project, we can run the following command:

mvn dependency:tree -Dincludes=junixsocket:junixsocket-core

The previous code should return:

[INFO]
[INFO] ------------------------< redis.clients:jedis >-------------------------
[INFO] Building Jedis 4.3.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ jedis ---

To exclude a dependency from the tree, we can use the -Dincludes parameter as shown in the syntax below.:

mvn dependency:tree -Dincludes=[groupId]:[artifactId]:[type]:[version]

For example:

mvn dependency:tree -Dexcludes=junixsocket:junixsocket-core

This should return the output as shown below:

Maven Save Dependency to File

You can also save the dependency tree to a file using the -DoutputFile parameter. An example is shown below:

mvn dependency:tree -DoutputFile="dep.tree"

In the previous command, we instruct Maven to create a dependency tree and save it into a file called dep.tree.

The resulting file output is provided below:

+- org.slf4j:slf4j-api:jar:1.7.32:compile
+- org.apache.commons:commons-pool2:jar:2.11.1:compile
+- org.json:json:jar:20211205:compile
+- com.google.code.gson:gson:jar:2.8.9:compile
+- junit:junit:jar:4.13.2:test
|  \- org.hamcrest:hamcrest-core:jar:1.3:test
+- org.slf4j:slf4j-simple:jar:1.7.32:test
+- com.kohlschutter.junixsocket:junixsocket-core:pom:2.4.0:test
|  +- com.kohlschutter.junixsocket:junixsocket-native-common:jar:2.4.0:test
|  \- com.kohlschutter.junixsocket:junixsocket-common:jar:2.4.0:test
\- org.mockito:mockito-inline:jar:3.12.4:test
\- org.mockito:mockito-core:jar:3.12.4:test
+- net.bytebuddy:byte-buddy:jar:1.11.13:test
+- net.bytebuddy:byte-buddy-agent:jar:1.11.13:test
\- org.objenesis:objenesis:jar:3.2:test

Maven will only include the project dependencies in the file in a hierarchical manner.

Conclusion

This post covered how to view the Maven dependency tree using the Maven dependency plugin.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list