How to exclude older versions of maven dependencies and use the new version?

I work with a Maven project, and I have two projects: ProjectA and ProjectB . My ProjectA is a maven library whose pom looks like this:

 <?xml version="1.0" encoding="UTF-8"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.texture.partial</groupId> <artifactId>PartialPlatform</artifactId> <version>2.1.5-RELEASE</version> </parent> <groupId>com.texture.transform.golden</groupId> <artifactId>SampleClient</artifactId> <version>1.0.4</version> <dependencies> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.2.4</version> </dependency> <dependency> <groupId>com.texture.partial.core</groupId> <artifactId>PartialKernel</artifactId> </dependency> <dependency> <groupId>com.texture.webres</groupId> <artifactId>WebResPartial</artifactId> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>com.texture.kernel</groupId> <artifactId>TextureServer</artifactId> </dependency> <dependency> <groupId>com.texture.kernel</groupId> <artifactId>Kernel</artifactId> </dependency> <dependency> <groupId>com.texture.v3jars.Houston</groupId> <artifactId>KernelDAL</artifactId> </dependency> <dependency> <groupId>com.texture.kernel</groupId> <artifactId>uKernel</artifactId> </dependency> <dependency> <groupId>com.texture.kernel</groupId> <artifactId>uKernelCore</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-asm</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> </dependency> <dependency> <groupId>aopalliance</groupId> <artifactId>aopalliance</artifactId> </dependency> <dependency> <groupId>org.apache.servicemix.bundles</groupId> <artifactId>org.apache.servicemix.bundles.cglib</artifactId> </dependency> <dependency> <groupId>com.texture.partial.core</groupId> <artifactId>ConfigWeb</artifactId> </dependency> <dependency> <groupId>com.texture.partial.core</groupId> <artifactId>PartialWeb</artifactId> </dependency> <dependency> <groupId>com.googlecode.jmockit</groupId> <artifactId>jmockit</artifactId> <version>1.7</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-surefire-plugin</artifactId> <configuration> <argLine>-javaagent:"${settings.localRepository}"/com/googlecode/jmockit/jmockit/1.7/jmockit-1.7.jar</argLine> </configuration> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>cobertura-maven-plugin</artifactId> <configuration> <instrumentation> <excludes> <exclude>**/test/**/*.class</exclude> </excludes> </instrumentation> <formats> <format>xml</format> <format>html</format> </formats> </configuration> </plugin> </plugins> </build> </project> 

My above pom PartialKernel uses an older version of various Spring Framework dependencies like spring-core , spring-web . It brings version 3.2.8.RELEASE , and I want to use the latest version of these two Spring frames, which is 4.1.6.RELEASE . What is the correct way to exclude older versions of spring-core and spring-web from PartialKernel and use the latest version?

  <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.1.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.1.6.RELEASE</version> </dependency> 

I need to use the latest version, as some classes exist only in the latest version.

+6
source share
1 answer

There may be incompatible differences between the version of the library that the dependency requires and the one you want to use. If you are happy to accept this risk, you can use maven exclusions to ignore transitive dependencies.

You can exclude, for example, spring-core from PartialKernel by adding:

 <dependency> <groupId>com.texture.partial.core</groupId> <artifactId>PartialKernel</artifactId> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> </exclusion> </exclusions> </dependency> 

Note that you will need to do this for every dependency that makes spring dependencies.

Now define the version of spring-core that you want to use in the top level pom dependency management :

 <dependencyManagement> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.1.6.RELEASE</version> </dependency> </dependencyManagement> 
+7
source

Source: https://habr.com/ru/post/985477/


All Articles