You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot结合Maven Shade Plugin替换依赖类问题求助

解决Spring Boot项目中用Maven Shade插件替换依赖类的常见问题

针对你遇到的找不到主类、获取嵌套归档失败、签名文件摘要无效问题,调整后的Maven Shade插件配置如下,同时附上问题原因和修复说明:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.2.4</version> <!-- 指定稳定版本,避免兼容性问题 -->
    <configuration>
        <keepDependenciesWithProvidedScope>true</keepDependenciesWithProvidedScope>
        <createDependencyReducedPom>true</createDependencyReducedPom>
        <filters>
            <!-- 全局排除签名文件,解决签名摘要无效问题 -->
            <filter>
                <artifact>*:*</artifact>
                <excludes>
                    <exclude>META-INF/*.SF</exclude>
                    <exclude>META-INF/*.DSA</exclude>
                    <exclude>META-INF/*.RSA</exclude>
                    <exclude>META-INF/*.MF</exclude> <!-- 额外排除可能冲突的MANIFEST文件 -->
                </excludes>
            </filter>
            <!-- 排除目标依赖中的待替换类 -->
            <filter>
                <artifact>dep-to-replace-class-in</artifact>
                <excludes>
                    <exclude>xyz/morecraft/dev/mcds/processor/ias/mq/MqConnectorBase.class</exclude>
                </excludes>
            </filter>
        </filters>
    </configuration>
    <executions>
        <execution>
            <id>shade</id>
            <goals>
                <goal>shade</goal>
            </goals>
            <phase>package</phase>
            <configuration>
                <transformers>
                    <!-- Spring Boot专用Transformer,处理嵌套归档和主类问题 -->
                    <transformer implementation="org.springframework.boot.maven.ShadeSpringBootTransformer"/>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                        <resource>META-INF/spring.handlers</resource>
                    </transformer>
                    <transformer implementation="org.springframework.boot.maven.PropertiesMergingResourceTransformer">
                        <resource>META-INF/spring.factories</resource>
                    </transformer>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                        <resource>META-INF/spring.schemas</resource>
                    </transformer>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                </transformers>
            </configuration>
        </execution>
    </executions>
</plugin>

<!-- 保留Spring Boot插件用于常规构建,避免冲突 -->
<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>2.2.7.RELEASE</version>
    <executions>
        <execution>
            <id>repackage</id>
            <goals>
                <goal>repackage</goal>
            </goals>
            <phase>package</phase>
            <!-- 禁用默认repackage,让Shade插件处理最终打包 -->
            <configuration>
                <skip>true</skip>
            </configuration>
        </execution>
    </executions>
</plugin>

关键修复说明:

  • 解决签名文件摘要无效:全局排除所有依赖的签名文件(SF/DSA/RSA),同时额外排除MF文件避免MANIFEST冲突,确保打包后不会出现签名校验错误。
  • 解决找不到主类/嵌套归档失败:
    1. 使用org.springframework.boot.maven.ShadeSpringBootTransformer替代手动配置ManifestTransformer,这个Transformer专门适配Spring Boot的嵌套归档结构,自动处理主类和BOOT-INF目录的构建逻辑。
    2. 单独配置Spring Boot插件并跳过其默认的repackage目标,避免和Shade插件的打包流程冲突。
  • 配置优化:
    • 给Shade插件指定明确的稳定版本(3.2.4和Spring Boot 2.2.7兼容性良好),避免版本兼容问题。
    • 将排除待替换类的过滤器移到全局configuration中,确保在打包全过程生效,而不是仅在execution阶段。

额外注意事项:

  • 确保你的自定义类xyz.morecraft.dev.mcds.processor.ias.mq.MqConnectorBase的包路径和原依赖类完全一致,这样才能在类加载时优先加载你的实现。
  • 执行打包时使用mvn clean package,确保清理旧的构建产物。

内容的提问来源于stack exchange,提问作者Mateusz Stefaniak

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 14:05:23