Transitive Dependency
About
A transitive dependency in Maven is a dependency that our project does not explicitly declare but is included automatically because another dependency requires it.
For example, if our project depends on Spring Boot Starter Web, it will automatically include Spring MVC, Tomcat, and Jackson without us having to specify them individually.
Why Do We Need Transitive Dependencies?
Maven's transitive dependency mechanism helps in:
Reducing Manual Effort – No need to specify every required library.
Avoiding Compatibility Issues – Ensures all dependencies work together.
Maintaining a Clean
pom.xml
– Only direct dependencies need to be declared.
How Transitive Dependencies Work?
Maven resolves dependencies using a dependency tree, where
Direct Dependencies – Explicitly declared in your
pom.xml
.Transitive Dependencies – Automatically included based on direct dependencies.
Example: Understanding Transitive Dependencies
Let's say we add Spring Boot Starter Web to our project:
This dependency internally requires:
Spring MVC
Jackson (for JSON processing)
Tomcat (embedded web server)
We don’t need to specify these separately; Maven will fetch them automatically.
How to View Transitive Dependencies?
Maven provides a command to analyze dependency trees:
Example Output
This means
We included spring-boot-starter-web
It pulled Spring Boot Starter JSON, Jackson, and Tomcat Embed Core
How to Manage Transitive Dependencies?
A. Excluding Unwanted Transitive Dependencies
Sometimes, a transitive dependency might cause conflicts or be unnecessary. We can exclude it using
This removes Tomcat, useful if we are using another web server like Jetty.
B. Forcing a Specific Version of a Transitive Dependency
If a transitive dependency has an older version, we can override it in dependencyManagement
:
This ensures Jackson uses version 2.14.0, even if a lower version is pulled.
Last updated
Was this helpful?