Maven: How to Remove/Ignore Transitive Dependency
- By Viral Patel on September 12, 2012
If you use Maven in your project for dependency management and if you use it a lot then sometimes you get into weird classpath issues where more than one version of JAR is loaded. Most of times I have got into issues related to cglib library. If two versions of cglibs, like cglib-2.1.1.jar and cglib-nodep.jar are present in your classpath then its likely you get into trouble.
Maven automatically includes the transitive dependencies in your project. This can cause different versions of same JAR to be fetched.
To avoid this, you can use Maven’s
tag. Consider below Maven dependency of spring-json library.
net.sf.spring-json spring-json 1.2
Adding this dependency of spring-json library would include following jar files automatically.
cglib-full 2.0.2 spring 2.0.2 xstream 1.3 commons-beanutils 1.7.0 commons-collections 3.2 commons-lang 2.2 servlet-api 2.5 junit 4.0 ezmorph 1.0.4 json-lib 2.2.3 sojo-optional 0.5.0 jettison 1.0.1 spring-mock 2.0.6 stax-api 1.0.1
Note how cglib-full 2.0.2 and spring 2.0.2 are fetched! To avoid this, and to ignore any transitive dependency simple use below code:
net.sf.spring-json spring-json 1.2 cglib cglib-full org.springframework spring
Note how we specified
tag to remove spring and cglib dependencies from the transitive dependency tree.
I hope this helps.
Get our Articles via Email. Enter your email address.