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>
相关推荐
心灵宝贝2 小时前
IDEA 安装 SpotBugs 插件超简单教程
java·macos·intellij-idea
幼稚诠释青春2 小时前
Java学习笔记(对象)
java·开发语言
小羊学伽瓦3 小时前
【Java基础】——JVM
java·jvm
老任与码3 小时前
Spring AI(2)—— 发送消息的API
java·人工智能·spring ai
*.✧屠苏隐遥(ノ◕ヮ◕)ノ*.✧3 小时前
MyBatis快速入门——实操
java·spring boot·spring·intellij-idea·mybatis·intellij idea
csdn_freak_dd3 小时前
查看单元测试覆盖率
java·单元测试
爱吃烤鸡翅的酸菜鱼3 小时前
【SpringMVC】详解cookie,session及实战
java·http·java-ee·intellij-idea
Wyc724093 小时前
JDBC:java与数据库连接,Maven,MyBatis
java·开发语言·数据库
老任与码3 小时前
Spring AI(3)——Chat Memory
java·人工智能·spring ai
贺函不是涵4 小时前
【沉浸式求职学习day36】【初识Maven】
java·学习·maven