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
<exclusions> tag. Consider below Maven dependency of spring-json library.
<dependency> <groupId>net.sf.spring-json</groupId> <artifactId>spring-json</artifactId> <version>1.2</version> </dependency>
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:
<dependency> <groupId>net.sf.spring-json</groupId> <artifactId>spring-json</artifactId> <version>1.2</version> <exclusions> <exclusion> <groupId>cglib</groupId> <artifactId>cglib-full</artifactId> </exclusion> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> </exclusions> </dependency>
Note how we specified
<exclusion> 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.