Maven dependency example explained
Maven serves as a project build tool for both Java and Android applications. One standout feature that distinguishes Maven from the ant tool is its robust dependencies management system for software projects.
When utilizing Maven in your project, it’s essential to specify dependencies in the pom.xml file. Once configured, these dependencies are automatically downloaded from repositories to your local machine during first run.
During the installation process with Maven, the plugin is employed to configure and manipulate the necessary artifacts.
Below is a sample code snippet demonstrating how to configure spring-core
as a dependency in the pom.xml
file of an application:“
<project>
<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-core</artifactid>
<version>2.5.6</version>
</dependency>
</project>
When executing the compile
or install
command in Maven, your project fetches the spring-core
JAR for the first time from the Spring repository server. To enable this, you must configure the repository server in the pom.xml
file using the following code snippet
<repositories>
<repository>
<id>spring-release</id>
<url>http://repository.springsource.com/maven/bundles/release</url>
</repository>
</repositories>
You can configure multiple repositories in the pom.xml, and the order in which repositories are listed determines the sequence in which Maven looks for the artifact.
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 serves as a fundamental introduction to exploring Maven dependency examples. Hopefully, you now have enough information to kickstart your journey.