maven shade 解决jar冲突问题

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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值