Maven Dependency Type
Introduction
When working with maven you can set your dependency direct on pom.xml
what maven is in the background, how to call the dependency when building your project, and how it works. you can know this information about maven with this article.
Thanks for https://reflectoring.io/maven-bom/ to important information about maven
Type of Dependency:
- direct dependencies
that are explicitly included in our Project Object Model (pom.xml
) file in the <dependencies>
section. They can be added using the <dependency>
tag. Here is an example of a logging library added to a pom.xml
file:
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
- transitive dependencies:
A project that we include as a dependency in our project, like the logging library above, can declare its own dependencies in a pom.xml
file. These dependencies are then considered transitive dependencies to our project. When Maven pulls a direct dependency, it also pulls its transitive dependencies.
How does Maven work with transitive dependencies?
For example, we will use one dependency for example we can use the spring dependency.
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<version>2.7.7</version>
</dependency>
When building a maven project the maven will install and prepare dependencies from the maven repository center if not exist on the local machine inside .m2
file
If using this command you can trace your dependency
mvn dependency:tree -Dverbose=true
You can see something like this after running the above command.
We started from one dependency, and in this output, we find out that Maven pulled additional dependencies. These additional dependencies are simply transitive.
conclusion
We need to know how is the maven work, to resolve our dependency and save time and effort, with resolve errors from maven.