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

升级Java至21后多模块项目中部分子模块Kotlin编译失败求助

升级Java至21后多模块项目中部分子模块Kotlin编译失败求助

我有一个多模块Maven项目,原本在Java 8 + Kotlin 2.3.0环境下运行正常,最近升级到Java 21后,其中一个子模块的Kotlin编译失败,但另一个子模块编译成功。

相关配置信息

父POM中的Kotlin Maven插件配置

<plugin>
    <groupId>org.jetbrains.kotlin</groupId>
    <artifactId>kotlin-maven-plugin</artifactId>
    <version>${kotlin.version}</version>
    <configuration>
        <args>
            <arg>-Xjsr305=strict</arg>
            <arg>-Xcontext-parameters</arg>
            <arg>-Xcontext-sensitive-resolution</arg>
            <arg>-Xannotation-default-target=param-property</arg>
            <arg>-Xconsistent-data-class-copy-visibility</arg>
            <arg>-Xdata-flow-based-exhaustiveness</arg>
            <arg>-Xallow-reified-type-in-catch</arg>
            <arg>-Xreturn-value-checker=check</arg>
        </args>
        <languageVersion>2.3</languageVersion>
        <jvmTarget>${java.version}</jvmTarget>
        <javaParameters>true</javaParameters>
        <compilerPlugins>
            <plugin>spring</plugin>
            <plugin>jpa</plugin>
        </compilerPlugins>
    </configuration>
    <executions>
        <execution>
            <id>kotlin-compile</id>
            <phase>compile</phase>
            <goals>
                <goal>compile</goal>
            </goals>
            <configuration>
                <sourceDirs>
                    <sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
                    <sourceDir>${project.basedir}/src/main/java</sourceDir>
                </sourceDirs>
            </configuration>
        </execution>
        <execution>
            <id>kotlin-test-compile</id>
            <phase>test-compile</phase>
            <goals>
                <goal>test-compile</goal>
            </goals>
            <configuration>
                <sourceDirs>
                    <sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
                </sourceDirs>
            </configuration>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-maven-allopen</artifactId>
            <version>${kotlin.version}</version>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-maven-noarg</artifactId>
            <version>${kotlin.version}</version>
        </dependency>
    </dependencies>
</plugin>

父POM中的Maven Compiler插件配置

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.15.0</version>
    <configuration>
        <release>${java.version}</release>
        <encoding>${project.build.sourceEncoding}</encoding>
        <compilerArgs>
            <arg>-parameters</arg>
            <arg>-Xlint:all,-unchecked,-varargs,-rawtypes</arg>
        </compilerArgs>
    </configuration>
    <executions>
        <!-- Replacing default-compile as it is treated specially by maven -->
        <execution>
            <id>default-compile</id>
            <phase>none</phase>
        </execution>
        <!-- Replacing default-testCompile as it is treated specially by maven -->
        <execution>
            <id>default-testCompile</id>
            <phase>none</phase>
        </execution>
        <execution>
            <id>java-compile</id>
            <phase>compile</phase>
            <goals>
                <goal>compile</goal>
            </goals>
        </execution>
        <execution>
            <id>java-test-compile</id>
            <phase>test-compile</phase>
            <goals>
                <goal>testCompile</goal>
            </goals>
        </execution>
    </executions>
</plugin>

问题子模块的插件配置

<plugin>
    <groupId>org.jetbrains.kotlin</groupId>
    <artifactId>kotlin-maven-plugin</artifactId>
</plugin>

成功子模块的插件配置

<plugin>
    <groupId>org.jetbrains.kotlin</groupId>
    <artifactId>kotlin-maven-plugin</artifactId>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <executions>
        <execution>
            <id>java-compile</id>
            <phase>compile</phase>
            <goals>
                <goal>compile</goal>
            </goals>
        </execution>
    </executions>
</plugin>

编译错误示例

编译失败时出现大量类似以下的错误:

  • Unresolved reference 'toInstant'. (java.util.Date().toInstant)
  • Argument type mismatch: actual type is '(Mutable)List!', but '(Mutable)List!' was expected.
  • Unresolved reference 'isAfter'

环境与配置验证信息

mvn -version 输出

Apache Maven 3.9.13 (39d686bd50d8e054301e3a68ad44781df6f80dda)
Maven home: /opt/maven
Java version: 21.0.7, vendor: Azul Systems, Inc., runtime: /usr/lib/jvm/zulu-21-amd64
OS name: "linux", version: "6.11.0-25-generic", arch: "amd64", family: "unix"

mvn clean compile -DskipTests=true -X | grep jvmTarget 输出

可以确认jvmTarget已正确设置为21:

<jvmTarget>21</jvmTarget>
[DEBUG] kotlin.compiler.jvmTarget: 21
[DEBUG]   (f) jvmTarget = 21
  <jvmTarget>21</jvmTarget>
[DEBUG] kotlin.compiler.jvmTarget: 21
[DEBUG]   (f) jvmTarget = 21
  <jvmTarget>21</jvmTarget>
[DEBUG] kotlin.compiler.jvmTarget: 21
[DEBUG]   (f) jvmTarget = 21

我尝试过直接在问题子模块中完全复制父POM的插件配置,但仍然无法解决问题,求各位大佬给点建议!


可能的原因与解决建议

结合你的错误信息和配置细节,我整理了几个可以优先排查的方向:

  1. 确保Kotlin标准库与Java 21适配
    Kotlin 2.3虽然支持Java 21,但要明确依赖适配高版本Java的Kotlin标准库。在父POM或问题子模块的<dependencies>中添加:

    <dependency>
        <groupId>org.jetbrains.kotlin</groupId>
        <artifactId>kotlin-stdlib-jdk21</artifactId>
        <version>${kotlin.version}</version>
    </dependency>
    

    旧的kotlin-stdlib-jdk8可能无法正确映射Java 21中的API扩展方法(比如Date.toInstant()),这很可能是你遇到方法找不到错误的核心原因。

  2. 同步问题子模块的Maven Compiler执行配置
    成功的子模块重新声明了maven-compiler-plugin的java-compile执行,而问题子模块没有。虽然父POM已经全局配置了编译逻辑,但子模块可能存在插件执行顺序或配置继承的隐性问题。建议在问题子模块中添加和成功子模块完全一致的maven-compiler-plugin配置,确保Java代码编译步骤正确触发,与Kotlin编译流程协调。

  3. 清理Maven缓存并强制更新依赖
    本地Maven仓库的旧缓存可能导致依赖版本不匹配,执行以下命令彻底清理后重新编译:

    mvn clean install -U -DskipTests=true
    

    -U参数会强制Maven更新所有依赖,避免使用缓存的旧版本文件。

  4. 排查实验性编译器参数的兼容性
    你的Kotlin编译器配置了多个实验性参数(比如-Xallow-reified-type-in-catch、-Xreturn-value-checker=check),这些参数可能与Java 21的编译逻辑存在冲突。可以暂时注释掉所有实验性参数,只保留基础配置,看看编译是否恢复正常,再逐个重新添加参数,排查出具体冲突的选项。

  5. 验证子模块的父POM继承完整性
    运行以下命令生成问题子模块的实际生效POM,确认Kotlin和Maven Compiler的配置是否正确继承:

    mvn help:effective-pom -f 问题子模块路径/pom.xml
    

    检查输出中是否包含父POM的所有插件参数,避免因<parent>标签配置错误导致的继承失效。

  6. 检查依赖树中的版本冲突
    运行以下命令查看问题子模块的依赖树,排查是否有旧版本的Kotlin标准库或Java API依赖被意外引入:

    mvn dependency:tree -f 问题子模块路径/pom.xml
    

    如果发现冲突依赖,使用<exclusions>标签排除旧版本即可。


备注:内容来源于stack exchange,提问作者Sergey

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.13 18:39:49