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

Java 8升级至Java 17时Maven编译失败求助

将Maven项目从Java 1.8升级至Java 17并适配Checker Framework的问题

已执行的升级操作

  • 移除Java 1.8环境下的Checker Framework依赖:
<dependency>
    <groupId>org.checkerframework</groupId>
    <artifactId>jdk8</artifactId>
    <version>1.9.10</version>
    <scope>provided</scope>
    <optional>true</optional>
</dependency>
  • 添加JDK9+版本的Checker Framework POM配置:
<profiles>
 <profile>
      <id>checkerframework-jdk9orlater</id>
      <activation>
        <jdk>[9,)</jdk>
      </activation>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
              <fork>true</fork>
              <compilerArgs combine.children="append">
                <arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED</arg>
                <arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED</arg>
                <arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED</arg>
                <arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED</arg>
                <arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED</arg>
                <arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED</arg>
                <arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED</arg>
                <arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED</arg>
                <arg>-J--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED</arg>
              </compilerArgs>
            </configuration>
          </plugin>
        </plugins>
      </build>
      <properties>
        <!-- Needed for animal-sniffer-maven-plugin version 1.19 which is broken (version 1.20 is fixed). -->
        <animal.sniffer.skip>true</animal.sniffer.skip>
      </properties>
    </profile>
  </profiles>
  • 将所有Checker Framework相关依赖版本统一升级至3.36.0

当前环境信息

  • Maven版本:3.9.3(运行在Java 17.0.8环境)
  • Java版本:17.0.8 LTS
  • JAVA_HOME已配置为JDK 17路径

遇到的问题

  1. 使用旧版Checker Framework 1.9.10执行mvn clean install时编译失败,报错:
Compilation failure
[ERROR] error: SourceChecker.typeProcessingStart: unexpected Throwable (RuntimeException);
message: Could not determine version from property java.version=17.0.8;
invoke the compiler with -AprintErrorStack to see the stack trace
  1. 升级至Checker Framework 3.36.0后,仍出现Maven编译失败,报错LifecycleExecutionException,根源为CompilationFailureException

已尝试的解决方法

  • 修改maven-compiler-plugin的source和target为17,同时在POM properties中配置对应版本
  • 删除.m2仓库,重新执行mvn clean install
  • 清理IntelliJ缓存,项目结构已设置为Java 17

排查与修复建议

  1. 校验依赖版本统一性
    执行mvn dependency:tree命令查看项目依赖树,确保所有Checker Framework相关依赖(如checker-qual、checker等)都已升级到3.36.0,无旧版本依赖残留。

  2. 补充Checker Framework编译参数
    在maven-compiler-plugin的compilerArgs中添加Checker Framework的处理器配置,明确指定要启用的检查器,示例:

<compilerArgs combine.children="append">
  <!-- 原有add-exports/add-opens参数 -->
  <arg>-processor</arg>
  <arg>org.checkerframework.checker.nullness.NullnessChecker</arg>
  <!-- 按需添加其他检查器 -->
</compilerArgs>
  1. 获取完整错误栈
    执行mvn clean install -Dmaven.compiler.args="-AprintErrorStack",输出详细错误信息,定位具体的编译失败点(如代码中的检查违规、依赖冲突等)。

  2. 适配maven-compiler-plugin版本
    使用兼容Java 17的maven-compiler-plugin版本(推荐3.11.0及以上),示例配置:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <version>3.11.0</version>
  <configuration>
    <source>17</source>
    <target>17</target>
    <fork>true</fork>
    <!-- 其他已配置参数 -->
  </configuration>
</plugin>
  1. 检查代码兼容性
    Java 17相比1.8存在API废弃、模块系统访问限制等变化,结合Checker Framework的错误提示,逐一修复代码中的兼容性问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 11:24:54