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>
相关推荐
楠枬8 分钟前
网页五子棋——项目测试
java·功能测试·spring
庄小焱36 分钟前
Maven——Maven开发经验总结(1)
maven·项目构建·项目打包部署
胡图蛋.1 小时前
Spring MVC的执行流程步骤
java·spring
sevevty-seven1 小时前
spring MVC执行流程
java·开发语言·spring
Joeysoda1 小时前
JavaEE进阶(1) Spring Web MVC 注解和参数传递
java·spring boot·spring·servlet·mvc·maven
結城1 小时前
Maven中一些基础知识点
java·maven
半聋半瞎2 小时前
什么是死锁?构成死锁的条件&如何解决
java·开发语言
程序员南飞2 小时前
算法-数据结构(图)-DFS深度优先遍历
java·数据结构·算法·职场和发展·深度优先
没明白白2 小时前
Java 实现快速排序算法:一条快速通道,分而治之
java·算法·排序算法
無炆_2 小时前
IDEA-插件开发踩坑记录-第五坑-没有飞机场导致无法访问GITHUB导致的讨厌问题
java·github·intellij-idea