Maven dependency example explained
Maven
is a project build tool for java and android applications.
One of the features of Maven
over ant
tool is dependencies management for software projects.
When you are using maven in your project, you need to define the dependencies in pom.xml. Once the dependencies have been configured, they will be downloaded from repositories to your local machine.
When installing with Maven, you must use the dependency plugin to configure the dependencies, which manipulates the artifacts.
So here is the sample code snippet for configuring spring-core
as a dependency to your project in pom..xml of an application.
<project>
<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-core</artifactid>
<version>2.5.6</version>
</dependency>
</project>
When you run compile or install command in maven, your project downloads the spring-core jar for the first time from the spring repository server.
You also should have to configure the repository server in pom.xml
with the following piece of code.
<repositories>
<repository>
<id>spring-release</id>
<url>http://repository.springsource.com/maven/bundles/release</url>
</repository>
</repositories>
So you can configure multiple repositories, the order of repositories that looks for the artifact should have to mention in the order in pom.xml
What is a transitive dependency in maven?
For example, Take the scenario, what happened if the spring-core
artifact has log4j
as one of the dependencies.
Should the log4j
dependency also download into your local project?
Yes, all the direct dependencies should resolve automatically. Transitive dependency meaning is that all the dependencies resolve for your project
transitively` configured in pom.xml.

In this example, your project is configured only as spring-core
, but your project has indirect dependencies log4j
and JMX
which are dependent on spring-core
.
So maven resolves all these dependencies i.e all direct and indirect dependencies.
Your project 1.0.0
has spring-core-2.5.6
direct dependency, log4j-1.2.12
and jmx-1.0.2
indirect dependencies
As per your project needs, you want to use log4j-1.2.14
only, but as per the above spring-core
dependency, you got log4j-1.2.12
as a dependency. So you have to avoid the log4j-1.2.12
dependency.
For this, we have to use an exclusion tag in pom.xml in dependency tag.
Following is the code snippet
<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-core</artifactid>
<version>2.5.6</version>
<exclusions>
<exclusion>
<groupid>log4j</groupid>
<artifactid>log4j</artifactid>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupid>log4j </groupid>
<artifactid>log4j</artifactid>
<version>1.2.14</version>
</dependency>
How do you display the maven dependency tree of an application?
mvn dependency:tree
It displays a maven dependency tree of project dependencies.
It helps to resolve conflicts with versions of dependencies.
Print Dependency tree for plugins configured in your application?
resolve-plugins will give the plugin dependencies tree of configured dependencies.
mvn dependency:resolve-plugins
Alternatively, You can use the below maven command to print plugin dependencies.
mvn -X
Conclusion
This topic has been a basic start to exploring on maven dependency example. Hopefully, you have enough information to get started.
If you have any questions, please feel free to leave a comment and I will get back to you.