Kapt在使用OpenJDK 16时无法正常工作。

28

我有一个应用程序,使用Kotlin 1.5.0和Kapt进行mapstructs。我已经升级了JDK到16,但是我遇到了以下编译错误:

Failed to execute goal org.jetbrains.kotlin:kotlin-maven-plugin:1.5.0:kapt (kapt) on project X: Compilation failure
java.lang.IllegalAccessError: class org.jetbrains.kotlin.kapt3.base.KaptContext (in unnamed module @0x248e24b) cannot access class com.sun.tools.javac.util.Context (in module jdk.compiler) because module jdk.compiler does not export com.sun.tools.javac.util to unnamed module @0x248e24b
    at org.jetbrains.kotlin.kapt3.base.KaptContext.<init>(KaptContext.kt:29)
    at org.jetbrains.kotlin.kapt3.KaptContextForStubGeneration.<init>(KaptContextForStubGeneration.kt:40)
    at org.jetbrains.kotlin.kapt3.AbstractKapt3Extension.contextForStubGeneration(Kapt3Extension.kt:285)
    at org.jetbrains.kotlin.kapt3.AbstractKapt3Extension.analysisCompleted(Kapt3Extension.kt:169)
    at org.jetbrains.kotlin.kapt3.ClasspathBasedKapt3Extension.analysisCompleted(Kapt3Extension.kt:100)
    at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration$invokeExtensionsOnAnalysisComplete(TopDownAnalyzerFacadeForJVM.kt:111)
    at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration(TopDownAnalyzerFacadeForJVM.kt:121)
    at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration$default(TopDownAnalyzerFacadeForJVM.kt:85)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler$analyze$1.invoke(KotlinToJVMBytecodeCompiler.kt:514)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler$analyze$1.invoke(KotlinToJVMBytecodeCompiler.kt:505)
    at org.jetbrains.kotlin.cli.common.messages.AnalyzerWithCompilerReport.analyzeAndReport(AnalyzerWithCompilerReport.kt:112)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.analyze(KotlinToJVMBytecodeCompiler.kt:505)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules$cli(KotlinToJVMBytecodeCompiler.kt:189)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules$cli$default(KotlinToJVMBytecodeCompiler.kt:155)
    at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:169)
    at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:52)
    at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:88)
    at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:44)
    at org.jetbrains.kotlin.cli.common.CLITool.exec(CLITool.kt:98)
    at org.jetbrains.kotlin.maven.KotlinCompileMojoBase.execCompiler(KotlinCompileMojoBase.java:228)
    at org.jetbrains.kotlin.maven.K2JVMCompileMojo.execCompiler(K2JVMCompileMojo.java:237)
    at org.jetbrains.kotlin.maven.kapt.KaptJVMCompilerMojo.execCompiler(KaptJVMCompilerMojo.java:152)
    at org.jetbrains.kotlin.maven.kapt.KaptJVMCompilerMojo.execCompiler(KaptJVMCompilerMojo.java:44)
    at org.jetbrains.kotlin.maven.KotlinCompileMojoBase.execute(KotlinCompileMojoBase.java:209)
    at org.jetbrains.kotlin.maven.K2JVMCompileMojo.execute(K2JVMCompileMojo.java:222)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:210)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:957)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:289)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:193)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:347)
    at org.codehaus.classworlds.Launcher.main(Launcher.java:47)

我看到了一个与Kapt和JDK 16有关的问题: https://youtrack.jetbrains.com/issue/KT-45545,我正在尝试在kotlin-maven-plugin中使用--add-opens作为args选项,但似乎它们无法正常工作:

<plugin>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>kapt</id>
                    <goals>
                        <goal>kapt</goal>
                    </goals>
                    <configuration>
                        <sourceDirs>
                            <sourceDir>src/main/kotlin</sourceDir>
                            <sourceDir>src/main/java</sourceDir>
                        </sourceDirs>
                        <annotationProcessorPaths>
                            <annotationProcessorPath>
                                <groupId>org.mapstruct</groupId>
                                <artifactId>mapstruct-processor</artifactId>
                                <version>${mapstruct.version}</version>
                            </annotationProcessorPath>
                        </annotationProcessorPaths>
                    </configuration>
                </execution>
            </executions>
            <configuration>
                <args>
                    <arg>-Xjsr305=strict</arg>
                    <arg>-X--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED</arg>
                    <arg>-X--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED</arg>
                    <arg>-X--add-opens=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED</arg>
                    <arg>-X--add-opens=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED</arg>
                    <arg>-X--add-opens=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED</arg>
                    <arg>-X--add-opens=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED</arg>
                    <arg>-X--add-opens=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED</arg>
                    <arg>-X--add-opens=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED</arg>
                    <arg>-X--add-opens=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED</arg>
                    <arg>-X--add-opens=jdk.compiler/com.sun.tools.javac.jvm=ALL-UNNAMED</arg>
                </args>
                <compilerPlugins>
                    <plugin>spring</plugin>
                </compilerPlugins>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.jetbrains.kotlin</groupId>
                    <artifactId>kotlin-maven-allopen</artifactId>
                    <version>${kotlin.version}</version>
                </dependency>
            </dependencies>
</plugin>

当我再次使用这些参数选项执行maven时,它告诉我以下内容:
[INFO] --- kotlin-maven-plugin:1.5.0:kapt (kapt) @ X ---
[WARNING] Source root doesn't exist: /home/nico/IdeaProjects/X/X/src/main/java
[WARNING] 'tools.jar' was not found, kapt may work unreliably
[INFO] Applied plugin: 'spring'
[WARNING] Flag is not supported by this version of the compiler: -X--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED
[WARNING] Flag is not supported by this version of the compiler: -X--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED
[WARNING] Flag is not supported by this version of the compiler: -X--add-opens=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED
[WARNING] Flag is not supported by this version of the compiler: -X--add-opens=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED
[WARNING] Flag is not supported by this version of the compiler: -X--add-opens=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED
[WARNING] Flag is not supported by this version of the compiler: -X--add-opens=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED
[WARNING] Flag is not supported by this version of the compiler: -X--add-opens=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED
[WARNING] Flag is not supported by this version of the compiler: -X--add-opens=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED
[WARNING] Flag is not supported by this version of the compiler: -X--add-opens=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED
[WARNING] Flag is not supported by this version of the compiler: -X--add-opens=jdk.compiler/com.sun.tools.javac.jvm=ALL-UNNAMED
  • 我该如何将这些选项添加到kotlin-maven-plugin中?
  • 除了使用JDK 16和maven运行Kapt外,还有其他可能性吗?
5个回答

39

根据JetBrains的comment

gradle.properties中添加org.gradle.jvmargs=--illegal-access=permit

选项可以通过用空格分隔它们来组合:

org.gradle.jvmargs=-Xmx1536m --illegal-access=permit

我的 gradle.properties 已经包含了 org.gradle.jvmargs=-Xmx1536m,我该如何将这两个选项合并? - rmtheis
3
@rmtheis 您可以通过空格将选项组合在一起:org.gradle.jvmargs=-Xmx1536m --illegal-access=permit - Mikhail Sharin

13

2022年更新

如果您使用Kotlin 1.6.0+,则Kapt应该能够在JDK 16+上直接工作,而无需任何解决方法。

这个提交在Kotlin中引入了必要的更改。

原始回答

我如何将这些选项添加到kotlin-maven-plugin中?

不幸的是,您无法这样做。 kotlin-maven-plugin 不接受JVM参数。

是否有其他可能性在JDK 16和Maven上运行Kapt?

对我有用的是在项目中创建.mvn/jvm.config文件,并添加以下内容:

 --illegal-access=permit --add-opens jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.jvm=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED

在命令行中构建项目时它可以正常工作,但请注意,当您使用IDE工具管理Maven构建时可能无法正常工作,例如IntelliJ完全忽略了该文件


1
我正在使用JDK 11,并在从Groovy迁移到Gradle KTS后遇到了相同的错误。不确定这与我的迁移有何关系!即使降级到JDK8也无法解决问题! 添加上述行解决了该问题。 - Amir
在您的 build.gradle.kts 文件中添加以下内容:tasks.withType<org.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask>() .configureEach { listOf("util", "file", "main", "jvm", "processing", "comp", "tree", "api", "parser", "code") .flatMap { listOf("--add-opens", "jdk.compiler/com.sun.tools.javac.$it=ALL-UNNAMED") } .forEach(kaptProcessJvmArgs::add) } - krevelen

1

我宁愿使用JDK 16,因为它是我在服务器上使用的版本,所以降级到JDK 15不是一个选项。 - Nico
不幸的是,这是唯一的长期解决方案,因为在 JDK17 上,无论如何都无法运行 kapt。然而,只有 BUILD 需要在 JDK15(最理想情况)或更早版本上完成。构建结果可以部署在 JDK17 及更新版本上运行。 - Agent_L

0

我使用的是Mac Intel,kotlin版本为1.5,AndroidStudio最新的稳定版本,我遇到了相同的构建错误,我已经尝试添加

  • --illegal-access=permit
  • 强制使用org.xerial:sqlite-jdbc:3.34.0版本
  • 重新启动AndroidStudio,重新启动计算机

但是它们都没有起作用,有a答案说kotlin 1.6修复了这个问题,但我无法升级

然后我尝试下载一个新的Android Studio版本(beta版),它可以工作,没有更多的构建错误


0

当 Kotlin Gradle 插件版本升级到 1.6.0+ 时可工作。

classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.21'

Gradle的Kotlin插件


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接