我正在尝试使用 JEP 359: Records (Preview) Java 中的功能 IntelliJ 2020.1.1 RC.
我定义了这样一个类:
package work.basil.example;
import java.time.LocalTime;
public record LocalTimeRange(LocalTime start , LocalTime stop)
{
}
当我使用这个 LocalTimeRange
类在另一个类中运行 main 方法时,没问题。
当我执行 Maven install
时出现此错误:
Error:(6,8) java: records are a preview feature and are disabled by default.
➥ 我怎样才能帮助 Maven 完成它的 install
操作?
我使用了 Maven Quickstart Archetype , 版本 1.4。然后我编辑了 POM 以使用其各种依赖项的所有最新版本。
我有“项目结构”设置:
Project Settings > Project > Project SDK > 14
Project Settings > Project > Project language level > 14 (Preview) - Records, patterns, text blocks
Project Settings > Modules > Project language level > 14 (Preview) - Records, patterns, text blocks
我有“首选项”设置:
Build, Execution, Deployment > Compiler > Java Compiler > Per-module bytecode version > Target bytecode version > 14
运行此 Java: openjdk 14.0.1 2020-04-14 OpenJDK 运行时环境 AdoptOpenJDK (build 14.0.1+7) OpenJDK 64-Bit Server VM AdoptOpenJDK (build 14.0.1+7, mixed mode, sharing)
使用:
IntelliJ IDEA 2020.1.1(终极版)
构建 #IU-201.7223.58,构建于 2020 年 4 月 26 日
订阅有效期至 2020 年 8 月 28 日
运行时版本:11.0.6+8-b765.40 x86_64
VM:JetBrains s.r.o 的 OpenJDK 64 位服务器 VM macOS 10.14.6
GC: ParNew, ConcurrentMarkSweep
内存:2200M
核心数:6
非捆绑插件:com.github.leomillon.uuidgenerator
最佳答案
这似乎是围绕 IntelliJ 2020.1.1 RC build #201 出现的新问题或错误。2020.1.1 最终版本中的行为相同。
参见工单# IDEA-237538,IntelliJ Build #IU-201.6668.121 no longer recognizes Java 14 records
解决方法:添加 <configuration>
元素
使您的 Maven clean
& install
成功完成,添加<configuration>
元素到你的两个 POM 元素,标记 --enable-preview
.
改变这个:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
</plugin>
……对此:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>14</release>
<compilerArgs>
<arg>--enable-preview</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
<configuration>
<argLine>--enable-preview</argLine>
</configuration>
</plugin>
关于java - 使用 Java 14 的 IntelliJ 2020.1 中的 "Records"预览功能在 Maven `install` 期间因编译器错误而失败,但以其他方式运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61471575/