{

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 dependency.

Should 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 projecttransitively` configured in pom.xml.

Understanding maven dependency  with example

In this example, your project is configured only 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 these 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.12as a dependency. So you have to avoid 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>  

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.

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 explore 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.

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.