Update:
Lombok v1.18.20 开箱即用地支持 JDK 16 https://github.com/rzwitserloot/lombok/issues/2681#issuecomment-812288829.
在同一个线程中,其中一位维护者还writes https://github.com/rzwitserloot/lombok/issues/2681#issuecomment-800423585:
我们有一些不太为人所知的漏洞,可以用来弥补一些差距。我们将同时开始开发 gradle 和 maven 插件,这将是一个长期修复。
Original:
您在最新的 JDK-16 版本中看到的异常是因为JEP 396:默认强封装 JDK 内部 https://openjdk.java.net/jeps/396。 Lombok 通过反射访问内部 JDK API,在以前的 Java 版本中这会导致警告消息,而现在会导致硬错误。
一般来说,在运行java时可以通过传递显式打开内部JDK包进行反射--add-opens=<module>/<package>=<accessing module>
运行时指令作为 VM 参数java
。在这种情况下,这些指令需要传递给java
调用时运行的进程javac
。这可以通过给传递给的选项添加前缀来完成javac
with -J
,这会将其传递给底层 JVM。
使用 Maven,我能够使其与以下编译器插件配置一起工作:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>16</source>
<target>16</target>
<!-- <release>16</release>-->
<fork>true</fork>
<compilerArgs>
<arg>--enable-preview</arg>
<arg>-Xlint:all</arg>
<arg>-J--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED</arg>
<arg>-J--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED</arg>
<arg>-J--add-opens=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED</arg>
<arg>-J--add-opens=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED</arg>
<arg>-J--add-opens=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED</arg>
<arg>-J--add-opens=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED</arg>
<arg>-J--add-opens=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED</arg>
<arg>-J--add-opens=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED</arg>
<arg>-J--add-opens=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED</arg>
<arg>-J--add-opens=jdk.compiler/com.sun.tools.javac.jvm=ALL-UNNAMED</arg>
</compilerArgs>
<!--for unmappable characters in classes-->
<encoding>UTF-8</encoding>
<showDeprecation>true</showDeprecation>
<showWarnings>true</showWarnings>
<!--for lombok annotations to resolve-->
<!--contradictory to maven, intelliJ fails with this-->
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
使用传递所需选项的地方<compilerArgs>
配置中的元素。
请注意,我添加了-J
在选项前面,以便将它们传递给正在运行的 JVMjavac
, 代替javac
选项。
在上面--add-opens
问题中列出的指令,附加:
-J--add-opens=jdk.compiler/com.sun.tools.javac.jvm=ALL-UNNAMED
也需要。
<fork>true</fork>
还需要,因为否则-J
选项被忽略(从输出来看mvn clean install -X
)。查看 Maven 文档,设置fork
to true
使用时似乎随时需要<compilerArgs>
:
https://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html#compilerArgs https://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html#compilerArgs
<compilerArgs>
设置要传递给编译器的参数,如果fork
被设定为true
.