Thursday, 3 February 2011

Maven dependency example explained

One of the feature 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 are defined , these will be downloaded into your local machine from repositories. To configure the dependencies , you have to use the dependency plugin which manipulates the artifcats when you are installing with maven
So here is sample code snippet for configuring spring-core to your project


org.springframework
spring-core
2.5.6





When you compiled, your project downloads spring-core jar for the first time from the spring repository server. You also should have to configure repository server in pom.xml with the following piece of code.


spring-release
http://repository.springsource.com/maven/bundles/release




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 transitive dependency in maven ?
For example,Take the scenario, what happened if 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 resolves for your project transitivily configured in pom.xml.



In this example, your project is configured only spring-core, but your project has indirect dependcies log4j and jmx which are depenendnt to spring-core. So maven resolves all these dependencies and your project has 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 above spring-core dependency you got log4j-1.2.12 as dependency. So you have to avoid log4j-1.2.12 dependency.

For this we have to use exclusion tag in pom.xml in dependency tag. Following is the code snippet


org.springframework
spring-core
2.5.6


log4j
log4j




log4j
log4j
1.2.14



This topic has been a very basic start to explore on maven dependency example. Hopefull 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.

0 comments:

Post a Comment