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>
相关推荐
都叫我大帅哥1 小时前
深入浅出 Resilience4j:Java 微服务的“免疫系统”实战指南
java·spring cloud
Cao_Shixin攻城狮3 小时前
Flutter运行Android项目时显示java版本不兼容(Unsupported class file major version 65)的处理
android·java·flutter
Dcs5 小时前
还在用 Arrays.hashCode?Java 自己也能写出更快的版本!
java
fouryears_234178 小时前
Spring,Spring Boot 和 Spring MVC 的关系以及区别
java·spring boot·spring·mvc
阿葱(聪)8 小时前
java 在k8s中的部署流程
java·开发语言·docker·kubernetes
浮生带你学Java8 小时前
2025Java面试题及答案整理( 2025年 7 月最新版,持续更新)
java·开发语言·数据库·面试·职场和发展
板板正9 小时前
SpringAI——提示词(Prompt)、提示词模板(PromptTemplate)
java·spring boot·ai·prompt
板板正9 小时前
SpringAI——对话记忆
java·spring boot·ai
期待のcode9 小时前
图片上传实现
java·前端·javascript·数据库·servlet·交互
李长渊哦9 小时前
深入理解Java中的Map.Entry接口
java·开发语言