含多版本JAR的WAR包部署失败问题求助
解决Jackson MRjar部署TomEE/Glassfish 6报错问题
问题本质
TomEE 9.1.1、Glassfish v6.2.5的类加载器未正确处理多版本JAR(MRjar)的META-INF/versions/目录,错误加载了Jackson 2.15.0中为Java 19编译的class文件(主版本号63),而服务器运行在Java 17环境下,导致版本不兼容报错。
方法1:替换为Jackson Java17专属版本
Jackson从2.13版本开始提供针对特定JDK的非MRjar包,直接替换依赖即可:
在pom.xml中修改Jackson相关依赖,添加jdk17分类器:
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.15.0</version> <classifier>jdk17</classifier> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.15.0</version> <classifier>jdk17</classifier> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.15.0</version> <classifier>jdk17</classifier> </dependency>
注意:若项目依赖其他Jackson模块(如jackson-modules-java8),需同步替换为带jdk17后缀的版本。
方法2:构建时移除MRjar中的Java19版本目录
若无法替换依赖,可通过Maven插件在构建过程中删除Jackson JAR里的高版本class目录:
在pom.xml的<build><plugins>中添加以下配置:
<!-- 解压Jackson依赖 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>unpack-jackson</id> <phase>prepare-package</phase> <goals><goal>unpack</goal></goals> <configuration> <artifactItems> <artifactItem> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.15.0</version> <outputDirectory>${project.build.directory}/jackson-core</outputDirectory> </artifactItem> <artifactItem> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.15.0</version> <outputDirectory>${project.build.directory}/jackson-databind</outputDirectory> </artifactItem> </artifactItems> </configuration> </execution> </executions> </plugin> <!-- 删除Java19版本的class目录 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-clean-plugin</artifactId> <executions> <execution> <id>remove-java19-classes</id> <phase>prepare-package</phase> <goals><goal>clean</goal></goals> <configuration> <filesets> <fileset> <directory>${project.build.directory}/jackson-core/META-INF/versions/19</directory> </fileset> <fileset> <directory>${project.build.directory}/jackson-databind/META-INF/versions/19</directory> </fileset> </filesets> </configuration> </execution> </executions> </plugin> <!-- 重新打包处理后的Jackson JAR --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <executions> <execution> <id>repackage-jackson-core</id> <phase>prepare-package</phase> <goals><goal>jar</goal></goals> <configuration> <classesDirectory>${project.build.directory}/jackson-core</classesDirectory> <outputDirectory>${project.build.directory}/dependency</outputDirectory> <finalName>jackson-core-2.15.0</finalName> </configuration> </execution> <execution> <id>repackage-jackson-databind</id> <phase>prepare-package</phase> <goals><goal>jar</goal></goals> <configuration> <classesDirectory>${project.build.directory}/jackson-databind</classesDirectory> <outputDirectory>${project.build.directory}/dependency</outputDirectory> <finalName>jackson-databind-2.15.0</finalName> </configuration> </execution> </executions> </plugin> <!-- 让WAR包使用处理后的Jackson JAR,排除原始依赖 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <configuration> <webResources> <resource> <directory>${project.build.directory}/dependency</directory> <targetPath>WEB-INF/lib</targetPath> </resource> </webResources> <packagingExcludes> WEB-INF/lib/jackson-core-2.15.0.jar, WEB-INF/lib/jackson-databind-2.15.0.jar </packagingExcludes> </configuration> </plugin>
该配置会在构建WAR包前,处理Jackson JAR并移除Java19版本的class,避免服务器类加载器误加载。
方法3:升级服务器版本
TomEE 10.x或Glassfish 7.x已修复MRjar的类加载问题,若允许升级服务器,直接部署即可解决。但如果受限于现有环境,优先考虑前两种方法。
内容的提问来源于stack exchange,提问作者user17022777
相关产品推荐
相关产品推荐

