maven Problem shading JAR的几个解决方案

1 现象

Error creating shaded jar: Problem shading JAR :xxxxxx.jar entry META-INF/versions/11/com/fasterxml/jackson/core/io/doubleparser /BigSignificand.class: java.lang.IllegalArgumentException -> [Help 1]

2 原因

这个问题通常是由于 maven-shade-plugin 在处理多版本 JAR 文件(Multi-Release JAR)时,无法正确处理 META-INF/versions 目录下的类文件。

META-INF/versions 是 Java 9 引入的多版本 JAR 文件特性,用于支持不同 Java 版本的类文件。

3 解决方法

3.1 方法1 升级 maven-shade-plugin 版本。

maven-shade-plugin 的较新版本已经对多版本 JAR 文件提供了更好的支持。尝试升级到最新版本(如 3.3.0 或更高版本)。

复制代码
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.3.0</version>            <!-- 使用较新版本 -->
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
        </execution>
    </executions>
</plugin>

3.2 方法2:排除冲突的依赖

如果升级插件版本无法解决问题,可以尝试排除 hadoop-huaweicloud 依赖中与 jackson-core 相关的冲突文件。

在 pom.xml 中添加排除规则:

复制代码
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.1.1</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <filters>
                    <filter>
                        <artifact>你的组:你的artifact</artifact>
                        <excludes>
                            <exclude>META-INF/versions/**</exclude>
                            <exclude>com/fasterxml/jackson/core/io/doubleparser/**</exclude>
                        </excludes>
                    </filter>
                </filters>
            </configuration>
        </execution>
    </executions>
</plugin>

3.3 方法3:显式统一依赖的版本

如果问题是由于 jackson-core 的版本冲突引起的,可以尝试统一项目中所有依赖的 jackson-core 版本。

在 pom.xml 中显式指定 jackson-core 的版本:

复制代码
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.13.0</version> <!-- 使用合适的版本 -->
</dependency>

3.4 方法4:禁用Shade插件的多版本支持

如果不需要多版本 JAR 文件的支持,可以通过配置maven-shade-plugin禁用对META-INF/versions 的处理。

在 pom.xml 中添加以下filter配置:

复制代码
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.1.1</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"/>
                </transformers>
                <filters>
                    <filter>
                        <artifact>*:*</artifact>
                        <excludes>
                            <exclude>META-INF/versions/**</exclude>
                        </excludes>
                    </filter>
                </filters>
            </configuration>
        </execution>
    </executions>
</plugin>
相关推荐
沉着的码农29 分钟前
【设计模式】基于责任链模式的参数校验
java·spring boot·分布式
Mr_Xuhhh41 分钟前
信号与槽的总结
java·开发语言·数据库·c++·qt·系统架构
纳兰青华1 小时前
bean注入的过程中,Property of ‘java.util.ArrayList‘ type cannot be injected by ‘List‘
java·开发语言·spring·list
coding and coffee1 小时前
狂神说 - Mybatis 学习笔记 --下
java·后端·mybatis
千楼1 小时前
阿里巴巴Java开发手册(1.3.0)
java·代码规范
reiraoy1 小时前
缓存解决方案
java
安之若素^1 小时前
启用不安全的HTTP方法
java·开发语言
ruanjiananquan992 小时前
c,c++语言的栈内存、堆内存及任意读写内存
java·c语言·c++
chuanauc2 小时前
Kubernets K8s 学习
java·学习·kubernetes
一头生产的驴2 小时前
java整合itext pdf实现自定义PDF文件格式导出
java·spring boot·pdf·itextpdf