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
相关产品推荐
相关产品推荐

