maven shade 解决jar冲突问题
解决jar冲突我们一般使用exclusoion 将其从依赖中排除。但是有情况下我们还需要include对应的一些依赖类,这个时候需要使用include的方式将对应的类打进jar内。 为了考虑jar冲突的问题,在jar中加入对应的类的同时需要使用relocation的方式将相关的类进行包名的relocation。 relocation中需要注意设置false 为false,这样relocation之前对应的类不会打进对应的jar内。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>xxxx</groupId>
<artifactId>xxxx</artifactId>
<version>xxxx</version>
<description></description>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>xxx</groupId>
<artifactId>xxx</artifactId>
<version>xxx</version>
<exclusions>
<!-- 从依赖中去除相关包 -->
<exclusion>
<artifactId>fastjson</artifactId>
<groupId>com.alibaba</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<!-- 使用relocation进行包名的替换,避免多个组件之间版本冲突的问题 -->
<relocations>
<relocation>
<pattern>com.fasterxml.jackson</pattern>
<shadedPattern>shaded.com.fasterxml.jackson</shadedPattern>
</relocation>
</relocations>
<artifactSet>
<includes>
<include>com.fasterxml.jackson.core:jackson-core</include>
<include>com.fasterxml.jackson.core:jackson-databind</include>
<include>com.fasterxml.jackson.core:jackson-annotations</include>
</includes>
</artifactSet>
<!-- 设置为false,避免将relocation前的类也打进包里 -->
<shadeSourcesContent>false</shadeSourcesContent>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>