In this tutorial, we are going to cover in-depth how to show maven dependency tree.

We will first start with some background on what is maven dependency plugin. Then, we will go down the rabbit hole to see how to print the dependency hierarchy of a given project.

Keep reading …

Maven Dependency Plugin

Apache has taken dependencies management to the next level by providing the dependency plugin. With this plugin, we can have a better understanding and control over the list of dependencies used in a specific project.

The plugin comes with several goals. However, in this post we will shed light only upon one goal: dependency:tree.

mvn dependency:tree Command

The main purpose of the dependency:tree goal is to display in form of a tree view all the dependencies of a given project.

To exemplify the use of this command, we are going to use the same project created in this article: build REST API with Spring Boot

Let’s see how the pom.xml of the project looks like:

    
        <properties>
            <java.version>1.8</java.version>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
        </dependencies>
    

Now, let’s run the mvn dependency:tree command and see the output:

    
        C:\Users\Asus\GIT\msproducts>mvn dependency:tree
        ....
        [INFO] com.restapi:msproducts:jar:0.0.1-SNAPSHOT
        [INFO] +- org.springframework.boot:spring-boot-starter-data-jpa:jar:2.2.6.RELEASE:compile
        [INFO] |  +- org.springframework.boot:spring-boot-starter-aop:jar:2.2.6.RELEASE:compile
        [INFO] |  |  +- org.springframework:spring-aop:jar:5.2.5.RELEASE:compile
        [INFO] |  |  \- org.aspectj:aspectjweaver:jar:1.9.5:compile
        [INFO] |  +- org.springframework.boot:spring-boot-starter-jdbc:jar:2.2.6.RELEASE:compile
        [INFO] |  |  +- com.zaxxer:HikariCP:jar:3.4.2:compile
        [INFO] |  |  \- org.springframework:spring-jdbc:jar:5.2.5.RELEASE:compile
        [INFO] |  +- jakarta.activation:jakarta.activation-api:jar:1.2.2:compile
        [INFO] |  +- jakarta.persistence:jakarta.persistence-api:jar:2.2.3:compile
        [INFO] |  +- jakarta.transaction:jakarta.transaction-api:jar:1.3.3:compile
        [INFO] |  +- org.hibernate:hibernate-core:jar:5.4.12.Final:compile
        [INFO] |  |  +- org.jboss.logging:jboss-logging:jar:3.4.1.Final:compile
        [INFO] |  |  +- org.javassist:javassist:jar:3.24.0-GA:compile
        [INFO] |  |  +- net.bytebuddy:byte-buddy:jar:1.10.8:compile
        [INFO] |  |  +- antlr:antlr:jar:2.7.7:compile
        [INFO] |  |  +- org.jboss:jandex:jar:2.1.1.Final:compile
        [INFO] |  |  +- com.fasterxml:classmate:jar:1.5.1:compile
        [INFO] |  |  +- org.dom4j:dom4j:jar:2.1.1:compile
        [INFO] |  |  +- org.hibernate.common:hibernate-commons-annotations:jar:5.1.0.Final:compile
        [INFO] |  |  \- org.glassfish.jaxb:jaxb-runtime:jar:2.3.2:compile
        [INFO] |  |     +- org.glassfish.jaxb:txw2:jar:2.3.2:compile
        [INFO] |  |     +- com.sun.istack:istack-commons-runtime:jar:3.0.8:compile
        [INFO] |  |     +- org.jvnet.staxex:stax-ex:jar:1.8.1:compile
        [INFO] |  |     \- com.sun.xml.fastinfoset:FastInfoset:jar:1.2.16:compile
        [INFO] |  +- org.springframework.data:spring-data-jpa:jar:2.2.6.RELEASE:compile
        [INFO] |  |  +- org.springframework.data:spring-data-commons:jar:2.2.6.RELEASE:compile
        [INFO] |  |  +- org.springframework:spring-orm:jar:5.2.5.RELEASE:compile
        [INFO] |  |  +- org.springframework:spring-context:jar:5.2.5.RELEASE:compile
        [INFO] |  |  +- org.springframework:spring-tx:jar:5.2.5.RELEASE:compile
        [INFO] |  |  +- org.springframework:spring-beans:jar:5.2.5.RELEASE:compile
        [INFO] |  |  \- org.slf4j:slf4j-api:jar:1.7.30:compile
        [INFO] |  \- org.springframework:spring-aspects:jar:5.2.5.RELEASE:compile
        [INFO] +- org.springframework.boot:spring-boot-starter-web:jar:2.2.6.RELEASE:compile
        [INFO] |  +- org.springframework.boot:spring-boot-starter:jar:2.2.6.RELEASE:compile
        [INFO] |  |  +- org.springframework.boot:spring-boot:jar:2.2.6.RELEASE:compile
        [INFO] |  |  +- org.springframework.boot:spring-boot-autoconfigure:jar:2.2.6.RELEASE:compile
        [INFO] |  |  +- org.springframework.boot:spring-boot-starter-logging:jar:2.2.6.RELEASE:compile
        [INFO] |  |  |  +- ch.qos.logback:logback-classic:jar:1.2.3:compile
        [INFO] |  |  |  |  \- ch.qos.logback:logback-core:jar:1.2.3:compile
        [INFO] |  |  |  +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.12.1:compile
        [INFO] |  |  |  |  \- org.apache.logging.log4j:log4j-api:jar:2.12.1:compile
        [INFO] |  |  |  \- org.slf4j:jul-to-slf4j:jar:1.7.30:compile
        [INFO] |  |  +- jakarta.annotation:jakarta.annotation-api:jar:1.3.5:compile
        [INFO] |  |  \- org.yaml:snakeyaml:jar:1.25:runtime
        [INFO] |  +- org.springframework.boot:spring-boot-starter-json:jar:2.2.6.RELEASE:compile
        [INFO] |  |  +- com.fasterxml.jackson.core:jackson-databind:jar:2.10.3:compile
        [INFO] |  |  |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.10.3:compile
        [INFO] |  |  |  \- com.fasterxml.jackson.core:jackson-core:jar:2.10.3:compile
        [INFO] |  |  +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.10.3:compile
        [INFO] |  |  +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.10.3:compile
        [INFO] |  |  \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.10.3:compile
        [INFO] |  +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.2.6.RELEASE:compile
        [INFO] |  |  +- org.apache.tomcat.embed:tomcat-embed-core:jar:9.0.33:compile
        [INFO] |  |  +- org.apache.tomcat.embed:tomcat-embed-el:jar:9.0.33:compile
        [INFO] |  |  \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:9.0.33:compile
        [INFO] |  +- org.springframework.boot:spring-boot-starter-validation:jar:2.2.6.RELEASE:compile
        [INFO] |  |  +- jakarta.validation:jakarta.validation-api:jar:2.0.2:compile
        [INFO] |  |  \- org.hibernate.validator:hibernate-validator:jar:6.0.18.Final:compile
        [INFO] |  +- org.springframework:spring-web:jar:5.2.5.RELEASE:compile
        [INFO] |  \- org.springframework:spring-webmvc:jar:5.2.5.RELEASE:compile
        [INFO] |     \- org.springframework:spring-expression:jar:5.2.5.RELEASE:compile
        [INFO] +- mysql:mysql-connector-java:jar:8.0.19:runtime
        [INFO] +- org.projectlombok:lombok:jar:1.18.12:compile (optional)
        [INFO] \- org.springframework.boot:spring-boot-starter-test:jar:2.2.6.RELEASE:test
        [INFO]    +- org.springframework.boot:spring-boot-test:jar:2.2.6.RELEASE:test
        [INFO]    +- org.springframework.boot:spring-boot-test-autoconfigure:jar:2.2.6.RELEASE:test
        [INFO]    +- com.jayway.jsonpath:json-path:jar:2.4.0:test
        [INFO]    |  \- net.minidev:json-smart:jar:2.3:test
        [INFO]    |     \- net.minidev:accessors-smart:jar:1.2:test
        [INFO]    |        \- org.ow2.asm:asm:jar:5.0.4:test
        [INFO]    +- jakarta.xml.bind:jakarta.xml.bind-api:jar:2.3.3:compile
        [INFO]    +- org.junit.jupiter:junit-jupiter:jar:5.5.2:test
        [INFO]    |  +- org.junit.jupiter:junit-jupiter-api:jar:5.5.2:test
        [INFO]    |  |  +- org.apiguardian:apiguardian-api:jar:1.1.0:test
        [INFO]    |  |  +- org.opentest4j:opentest4j:jar:1.2.0:test
        [INFO]    |  |  \- org.junit.platform:junit-platform-commons:jar:1.5.2:test
        [INFO]    |  +- org.junit.jupiter:junit-jupiter-params:jar:5.5.2:test
        [INFO]    |  \- org.junit.jupiter:junit-jupiter-engine:jar:5.5.2:test
        [INFO]    |     \- org.junit.platform:junit-platform-engine:jar:1.5.2:test
        [INFO]    +- org.mockito:mockito-junit-jupiter:jar:3.1.0:test
        [INFO]    +- org.assertj:assertj-core:jar:3.13.2:test
        [INFO]    +- org.hamcrest:hamcrest:jar:2.1:test
        [INFO]    +- org.mockito:mockito-core:jar:3.1.0:test
        [INFO]    |  +- net.bytebuddy:byte-buddy-agent:jar:1.10.8:test
        [INFO]    |  \- org.objenesis:objenesis:jar:2.6:test
        [INFO]    +- org.skyscreamer:jsonassert:jar:1.5.0:test
        [INFO]    |  \- com.vaadin.external.google:android-json:jar:0.0.20131108.vaadin1:test
        [INFO]    +- org.springframework:spring-core:jar:5.2.5.RELEASE:compile
        [INFO]    |  \- org.springframework:spring-jcl:jar:5.2.5.RELEASE:compile
        [INFO]    +- org.springframework:spring-test:jar:5.2.5.RELEASE:test
        [INFO]    \- org.xmlunit:xmlunit-core:jar:2.6.4:test
        [INFO] ------------------------------------------------------------------------
        [INFO] BUILD SUCCESS
        [INFO] ------------------------------------------------------------------------
        [INFO] Total time:  12.886 s
        [INFO] Finished at: 2021-06-17T15:15:36+02:00
        [INFO] ------------------------------------------------------------------------
    

As we can see, maven prints the dependencies tree of our project. Cool, right?

Please bear in mind that this command requires a maven project to be executed and of course maven installed and configured in your machine.

Another important thing to mention is that we can display the maven dependency hierarchy in the following formats: text which is the default format, DOT, GraphML, and TGF.

Maven Show Dependency Tree in Eclipse

Displaying the maven dependencies tree in Eclipse is pretty simple and takes less than 10 seconds.

To accomplish that, we need to open the pom.xml of our project and then click on the Dependency Hierarchy tab:

Show Maven Dependency Tree in Eclipse

Filter Dependencies

Sometimes, we may find ourselves working on a project that uses a lot of dependencies. So, managing all of them can be a real nightmare.

This is where the maven dependency plugin comes into the resue. dependency:tree comes with two filters to help us include or exclude specific dependencies while displaying the tree.

We can use the includes filter to specify a list of artifacts to include in the dependency hierarchy. Let’s filter our project’s tree to show only the lombok dependency:

    
        C:\Users\Asus\GIT\msproducts>mvn dependency:tree -Dincludes=org.projectlombok
        [INFO] Scanning for projects...
        [INFO]
        [INFO] -----------------------< com.restapi:msproducts >-----------------------
        [INFO] Building msproducts 0.0.1-SNAPSHOT
        [INFO] --------------------------------[ jar ]---------------------------------
        [INFO]
        [INFO] --- maven-dependency-plugin:3.1.2:tree (default-cli) @ msproducts ---
        [INFO] com.restapi:msproducts:jar:0.0.1-SNAPSHOT
        [INFO] \- org.projectlombok:lombok:jar:1.18.12:compile (optional)
        [INFO] ------------------------------------------------------------------------
        [INFO] BUILD SUCCESS
        [INFO] ------------------------------------------------------------------------
        [INFO] Total time:  1.944 s
        [INFO] Finished at: 2021-06-17T17:02:34+02:00
        [INFO] ------------------------------------------------------------------------
    

As shown above, we have successfully filtred the maven dependency tree to display only org.projectlombok:lombok.

Now, let’s see how we can remove artifacts from the tree. To do that, dependency:tree provides the excludes filter.

Let’s for example exclude the spring-boot-starter-data-jpa dependency from the tree:

    
        mvn dependency:tree -Dexcludes=org.springframework.boot:spring-boot-starter-data-jpa
    

We can also use multiple comma-separated patterns to exclude multiple artifacts:

    
        mvn dependency:tree -Dexcludes=org.springframework.*,org.projectlombok
    

Alternatively, we can use the <exclusions> namespace directly in pom.xml to tell maven to exclude dependencies.

For example, let’s remove Tomcat:

    
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    

So, resolving the tree once again will display all the dependencies except spring-boot-starter-tomcat.

Please keep in mind that we can use both includes and excludes parameters together to filter the dependency hierarchy.

Resolve Conflicts using Verbose Mode

Another interesting point about the maven dependency tree is the possibility to display and detects conflicts.

The verbose mode tells the tree plugin to show dependencies that are duplicates of another or in conflict with another’s version.

Save Maven Dependency Tree to a File

We can use the outputFile flag to save the result in a given file. Simarily, we can make use of the DoutputType to specify a custom format for the output:

    
        mvn dependency:tree -DoutputFile=myFile.dot -DoutputType=dot
    

Conclusion

To sum it up, in this post, we have explained in detail how to show maven dependency tree. We have also seen how to filter and divert the dependency hierarchy to a file.