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

Java WAR无主类项目配置ByteBuddy方法(附JAR项目配置参考)

WAR项目中使用ByteBuddy的maven-shade-plugin配置

问题背景

在带主类的JAR项目中,我使用以下maven-shade-plugin配置来整合ByteBuddy:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.2.4</version>
    <executions>
        <execution>
            <id>shade-my-jar</id>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <transformers>
                    <transformer
                            implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <manifestEntries>
                            <Main-Class>com.example.demo.SamplesJavaApplication</Main-Class>
                        </manifestEntries>
                    </transformer>
                </transformers>
                <filters>
                    <filter>
                        <artifact>*:*</artifact>
                        <excludes>
                            <exclude>META-INF/*.SF</exclude>
                            <exclude>META-INF/*.DSA</exclude>
                            <exclude>META-INF/*.RSA</exclude>
                        </excludes>
                    </filter>
                </filters>
            </configuration>
        </execution>
    </executions>
</plugin>

但Java WAR类型项目不存在主类,请问该如何配置以使用ByteBuddy?

解决方案

WAR项目的启动由Servlet容器负责,不需要指定Main-Class,只需移除原配置中与主类相关的Manifest配置即可,核心的shade打包逻辑和冲突规避规则可以保留:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.2.4</version>
    <executions>
        <execution>
            <id>shade-my-war</id>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <!-- 移除主类相关的Manifest配置,WAR无需指定启动类 -->
                <filters>
                    <filter>
                        <artifact>*:*</artifact>
                        <excludes>
                            <exclude>META-INF/*.SF</exclude>
                            <exclude>META-INF/*.DSA</exclude>
                            <exclude>META-INF/*.RSA</exclude>
                        </excludes>
                    </filter>
                </filters>
            </configuration>
        </execution>
    </executions>
</plugin>

配置说明

  • 保留filters节点:排除签名文件可以避免打包时出现签名冲突,这一规则对JAR和WAR项目均适用。
  • 移除ManifestResourceTransformer:WAR项目不需要自定义Manifest中的主类配置,因此直接删除该节点即可。
  • 执行逻辑不变:依然在package阶段执行shade目标,将ByteBuddy的依赖类打包到WAR的WEB-INF/lib目录或合并到项目类文件中,确保运行时可以正常加载ByteBuddy。

内容的提问来源于stack exchange,提问作者Dr Drake Ramoray

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 07:05:52