> For the complete documentation index, see [llms.txt](https://www.pranaypourkar.co.in/the-programmers-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.pranaypourkar.co.in/the-programmers-guide/maven/dependency-management/dependency-tree-and-analysis.md).

# Dependency Tree & Analysis

## About

A **dependency tree** in Maven is a hierarchical representation of all dependencies in a project, including **direct** and **transitive dependencies**. It helps to -

* Identify **dependency conflicts** (duplicate or incompatible versions).
* Understand **why a dependency is included** in the project.
* Optimize **dependency versions** to avoid unnecessary bloating.

#### **Example of a Dependency Tree**

Consider the following `pom.xml`:

```xml
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>3.1.0</version>
    </dependency>
    
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.17.1</version>
    </dependency>
</dependencies>
```

When you run the following command:

```sh
mvn dependency:tree
```

We will get output like this:

```
[INFO] com.example:my-project:jar:1.0.0
[INFO] ├── org.springframework.boot:spring-boot-starter-web:jar:3.1.0
[INFO] │   ├── org.springframework.boot:spring-boot-starter:jar:3.1.0
[INFO] │   │   ├── org.springframework.boot:spring-boot:jar:3.1.0
[INFO] │   │   ├── org.apache.logging.log4j:log4j-core:jar:2.14.1 (conflict)
[INFO] ├── org.apache.logging.log4j:log4j-core:jar:2.17.1 (direct)
```

**Notice** that `log4j-core` appears **twice**—once as **direct** and once as **transitive** (from `spring-boot-starter`).\
**Maven will use the nearest version** (2.17.1 in this case).

## Dependency Tree Components

#### **1. Direct Dependencies**

* These are **explicitly declared** in `pom.xml`.
* Example: `spring-boot-starter-web` and `log4j-core` are direct dependencies.

#### **2. Transitive Dependencies**

* These are **indirect dependencies** pulled in by other dependencies.
* Example: `spring-boot-starter-web` **pulls in** `log4j-core`.

#### **3. Conflict Resolution**

* When different versions of a library exist, Maven applies **Nearest-Wins Strategy**.

## Commands for Analyzing Dependencies

### **1. Checking Full Dependency Tree**

```sh
mvn dependency:tree
```

Displays **all dependencies**, including transitive ones.

### **2. Finding Why a Dependency Exists**

```sh
mvn dependency:tree -Dincludes=org.apache.logging.log4j:log4j-core
```

This command shows **which dependencies brought in `log4j-core`**.

### **3. Displaying Dependencies in a Graphical Format**

For better readability, generate a dependency graph:

```sh
mvn dependency:tree -DoutputType=dot -DoutputFile=dependency-graph.dot
```

Then use a tool like **Graphviz** to visualize it.

### **4. Checking Dependency Versions**

```sh
mvn dependency:resolve
```

Lists **resolved versions** of all dependencies.

### **5. Finding Dependency Conflicts**

```sh
mvn dependency:tree -Dverbose
```

Shows **conflicts** and which version Maven picks.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://www.pranaypourkar.co.in/the-programmers-guide/maven/dependency-management/dependency-tree-and-analysis.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
