Maven Command to Check Dependency Tree

Understanding dependencies and their hierarchy can be crucial when working with Maven projects. Maven provides a built-in command to visualize a project's dependency tree, which helps in identifying conflicts and understanding the structure of dependencies.

Why Check the Dependency Tree?

  1. Identify Conflicts: Detect conflicting versions of dependencies that might cause runtime issues.
  2. Understand Dependencies: Visualize how dependencies are related and the transitive dependencies that are included in your project.
  3. Debug Issues: Troubleshoot issues related to dependency resolution by inspecting the entire tree.

Command to Check Dependency Tree

The command to display the dependency tree in Maven is:

mvn dependency:tree

Example

Here's a step-by-step guide on how to use the dependency:tree command in a Maven project:

Step 1: Open Terminal or Command Prompt

Open your terminal or command prompt and navigate to the root directory of your Maven project.

Step 2: Run the Command

Execute the following command to display the dependency tree:

mvn dependency:tree

Output

The output will look something like this:

[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------< com.example:myapp >-------------------
[INFO] Building myapp 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:3.3.0:tree (default-cli) @ myapp ---
[INFO] com.example:myapp:jar:1.0-SNAPSHOT
[INFO] +- org.springframework:spring-core:jar:5.3.20:compile
[INFO] |  \- org.springframework:spring-jcl:jar:5.3.20:compile
[INFO] +- org.apache.commons:commons-lang3:jar:3.12.0:compile
[INFO] +- org.junit.jupiter:junit-jupiter:jar:5.9.1:test
[INFO] \- org.mockito:mockito-core:jar:5.1.1:test
[INFO]    \- net.bytebuddy:byte-buddy:jar:1.12.13:test
[INFO]    +- net.bytebuddy:byte-buddy-agent:jar:1.12.13:test
[INFO]    \- org.objenesis:objenesis:jar:3.3:test

In this example, the output shows the myapp project with its dependencies, including spring-core, commons-lang3, junit-jupiter, and mockito-core. The hierarchy and relationships between these dependencies are also displayed.

Filtering and Output Options

Maven's dependency:tree command supports various options to filter and customize the output. Here are some useful options:

Show Scope

To include the scope of each dependency in the tree, use:

mvn dependency:tree -Dverbose

Exclude Certain Dependencies

To exclude specific dependencies from the tree, use:

mvn dependency:tree -Dexcludes=groupId:artifactId

For example, to exclude commons-lang3, use:

mvn dependency:tree -Dexcludes=org.apache.commons:commons-lang3

Output to a File

To save the dependency tree to a file, use:

mvn dependency:tree -DoutputFile=dependency-tree.txt

Example Project POM

Here's an example pom.xml with the latest versions of the dependencies used in the example output:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>myapp</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.3.20</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>5.9.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>5.1.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

Conclusion

Checking the dependency tree in Maven is a valuable tool for managing and troubleshooting project dependencies. By using the mvn dependency:tree command, you can gain insights into the structure and relationships of your project's dependencies, helping to ensure a smooth and conflict-free build process.

For more in-depth learning about Maven, you can visit Java Guides Maven Tutorial.

Comments