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

如何解决Apache Camel 3生成Fat-Jar时的异常问题

解决方案:Apache Camel 3.18.2 构建可运行 Fat-Jar

优先推荐:使用 Camel 官方插件构建 Fat-Jar

Camel 官方提供的 camel-maven-plugin 内置了 fatjar 目标,专门适配 Camel 的依赖管理、SPI 服务加载和资源合并,是构建 Fat-Jar 最省心的方案,完全避免你遇到的 assembly/shade 插件问题。

完整 POM 配置片段

<build>
    <plugins>
        <!-- Camel 官方 FatJar 构建插件 -->
        <plugin>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-maven-plugin</artifactId>
            <version>3.18.2</version>
            <executions>
                <execution>
                    <goals>
                        <goal>fatjar</goal>
                    </goals>
                    <phase>package</phase>
                </execution>
            </executions>
            <configuration>
                <!-- 指定你的主类:如果是自定义 Main 子类,替换为你的类全路径 -->
                <mainClass>org.apache.camel.main.Main</mainClass>
                <!-- 可选:排除不需要的依赖,缩小 Jar 体积 -->
                <!--<excludes>
                    <exclude>com.example:unneeded-artifact</exclude>
                </excludes>-->
            </configuration>
        </plugin>

        <!-- 适配你的 JDK 版本(示例为 JDK 11) -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>11</source>
                <target>11</target>
            </configuration>
        </plugin>
    </plugins>
</build>

构建与运行

执行命令完成构建:

mvn clean package

构建完成后,Fat-Jar 会生成在 target 目录下,命名格式为 {项目名}-fatjar.jar,直接运行即可:

java -jar target/{项目名}-fatjar.jar

问题根源分析

1. maven-assembly-plugin 构建失败原因

你遇到的「无 assembly 描述符」错误是因为该插件需要手动指定描述符文件(如 src/main/assembly/assembly.xml),用来定义打包规则。即使补上描述符,也容易遗漏 Camel 依赖的 SPI 配置文件(如 META-INF/services 下的组件注册信息),进而触发 NoClassDefFoundError。

2. maven-shade-plugin 类型转换异常原因

Shade 插件默认会覆盖不同依赖中的同名配置文件,而 Camel 大量依赖 SPI 机制加载组件、转换器等。如果没有配置资源合并规则,会导致部分 Camel 服务实现丢失,运行时出现类型转换异常。若一定要用 Shade,需添加以下配置(不推荐,不如官方插件省心):

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.2.4</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <mainClass>org.apache.camel.main.Main</mainClass>
                <transformers>
                    <!-- 合并 SPI 服务注册文件 -->
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                    <!-- 合并 Spring 配置文件 -->
                    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                        <resource>META-INF/spring.handlers</resource>
                    </transformer>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                        <resource>META-INF/spring.schemas</resource>
                    </transformer>
                    <!-- 合并 Camel 类型转换器配置 -->
                    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                        <resource>META-INF/camel/type-converters</resource>
                    </transformer>
                </transformers>
            </configuration>
        </execution>
    </executions>
</plugin>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 21:40:37