仙桃市网站建设_网站建设公司_字体设计_seo优化
2025/12/18 11:56:29 网站建设 项目流程

一 JDK升级工具-EMT4J

1.1 工具介绍

EMT4J is a project that aims to simplify the Java version migration. At the moment, this project focuses on three LTS (i.e. Long-Term-Support) versions: 8, 11, 17 and 21. Therefore, if you want to migrate your application running on JDK 8/11/17 to JDK 11/17/21, then this project is for you.

EMT4J supports statically checking application artifacts including the project's classes and dependencies. It also supports running as a Java agent to perform runtime checking. During the checking process, EMT4J collects compatibility problems and outputs a report finally. It currently supports HTML, TEXT, and JSON formats. Users transform the project according to the report and finally complete the migration of the Java version.

Support for Java 21 and Autofix are introduced by version 0.9 which has not been released yet. If you want to use these two features, you need to manually install EMT4J to your local Maven repository.

1.2 工具使用

项目引入maven plugin

<build>

<plugins>

<plugin>

<groupId>org.eclipse.emt4j</groupId>

<artifactId>emt4j-maven-plugin</artifactId>

<version>0.8.0</version>

<executions>

<execution>

<phase>process-test-classes</phase>

<goals>

<goal>check</goal>

</goals>

</execution>

</executions>

<configuration>

<fromVersion>8</fromVersion>

<toVersion>11</toVersion>

<outputFile>report.html</outputFile>

</configuration>

</plugin>

</plugins>

</build>

执行命令

mvn process-test-classes

1.3 EMT4J报告

优先级

含义

风险程度

建议处理方式

P1

高优先级

极可能导致应用启动失败或运行时崩溃(如ClassNotFoundException、调用已删除方法)

必须修复,否则升级后大概率无法运行

P2

中优先级

可能导致功能异常或类加载问题

(如 ContextClassLoader 错误、反射失效)

建议修复,尤其在复杂应用(Spring Boot、Web 容器)中容易出问题

P3

低优先级

可能影响非核心功能,或仅在特定场景下有问题

可暂缓,上线前评估是否影响业务

P4

提示/信息级

通常是代码规范、内部 API 使用警告,不一定导致错误

可选修复,用于长期维护和合规

二 基于EMT4J报告JDK8升级21

2.1 构建新插件

官网提示

Support for Java 21 and Autofix are introduced by version 0.9 which has not been released yet. If you want to use these two features, you need to manually install EMT4J to your local Maven repository.

2.2 EMT4J报告问题修复案例

风险描述

测试用例

import com.xiaoleilu.hutool.lang.JarClassLoader;

import java.io.File;

public class TestJarClassLoader {

public static void main(String[] args) {

// 尝试实例化 JarClassLoader —— 这会触发对 system classloader 的 cast

try {

JarClassLoader.loadJarToSystemClassLoader(new File("test.jar"));

System.out.println("Load success!");

} catch (Exception e) {

e.printStackTrace();

}

}

}

执行结果

javac -cp "hutool-all-3.3.2.jar" TestJarClassLoader.java

java -cp ".:hutool-all-3.3.2.jar" TestJarClassLoader

java.lang.ClassCastException: class jdk.internal.loader.ClassLoaders$AppClassLoader cannot be cast to class java.net.URLClassLoader (jdk.internal.loader.ClassLoaders$AppClassLoader and java.net.URLClassLoader are in module java.base of loader 'bootstrap')

at com.xiaoleilu.hutool.lang.JarClassLoader.loadJarToSystemClassLoader(JarClassLoader.java:71)

at TestJarClassLoader.main(TestJarClassLoader.java:6)

升级建议

升级hutool版本

三 附录-Maven插件介绍

3.1 什么是插件

在 Maven 中,插件分为两种类型:构建插件(build plugins) 和 报告插件(reporting plugins)。

构建插件:在项目构建过程中执行,配置在 <build/> 元素中,用途是编译、测试、打包、部署等构建任务

报告插件:在站点(site)生成过程中执行,配置在 <reporting/> 元素中,用途是生成项目文档、代码质量报告、依赖分析等

3.2 Build插件配置

Build插件Mojo开发

Build插件maven配置

3.3 Hello world Build插件

@Mojo(name = "hello")
public class HelloWorldMojo extends AbstractMojo {
@Parameter(property = "msg", defaultValue = "false")
private String msg;

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
System.out.println("hello " + msg);
}
}

那么如何集成呢?欢迎评论区留下maven集成配置

四 常见问题

4.1 mvn配置问题

[ERROR] Please make sure you define the required toolchains in your ~/.m2/toolchains.xml file.

[ERROR] jdk [ version='8' ] [ERROR] Please make sure you define the required

解决方案

~/.m2/toolchains.xml

<?xml version="1.0" encoding="UTF-8"?>

<toolchains>

<!-- JDK toolchains -->

<toolchain>

<type>jdk</type>

<provides>

<version>8</version>

<vendor>openjdk</vendor>

</provides>

<configuration>

<jdkHome>/data/opt/jdk1.8.0_191</jdkHome>

</configuration>

</toolchain>

</toolchains>

五 参考链接

https://github.com/adoptium/emt4j

https://maven.apache.org/guides/mini/guide-configuring-plugins.html#Configuring_Build_Plugins

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询