“Records” preview feature in IntelliJ 2020.1 with Java 14 fails with compiler error during Maven `install`, but runs otherwise

别说谁变了你拦得住时间么 提交于 2020-06-23 11:01:53

问题


I am trying to use the JEP 359: Records (Preview) feature in Java with IntelliJ 2020.1.1 RC.

I defined a class like this:

package work.basil.example;

import java.time.LocalTime;

public record LocalTimeRange(LocalTime start , LocalTime stop)
{
}

When I run a main method in another class using this LocalTimeRange class, no problem.

When I do a Maven install I get this error:

Error:(6,8) java: records are a preview feature and are disabled by default.

➥ How can I help Maven complete its install operation?

I used the Maven Quickstart Archetype, version 1.4. I then edited the POM to use all the latest versions of its various dependencies.


I have "Project Structure" settings:

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

I have "Preferences" settings:

Build, Execution, Deployment > Compiler > Java Compiler > Per-module bytecode version > Target bytecode version > 14

Running this Java: openjdk 14.0.1 2020-04-14 OpenJDK Runtime Environment AdoptOpenJDK (build 14.0.1+7) OpenJDK 64-Bit Server VM AdoptOpenJDK (build 14.0.1+7, mixed mode, sharing)

Using:

IntelliJ IDEA 2020.1.1 (Ultimate Edition)

Build #IU-201.7223.58, built on April 26, 2020

Subscription is active until August 28, 2020

Runtime version: 11.0.6+8-b765.40 x86_64

VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o macOS 10.14.6

GC: ParNew, ConcurrentMarkSweep

Memory: 2200M

Cores: 6

Non-Bundled Plugins: com.github.leomillon.uuidgenerator


回答1:


This seems to be a fresh issue or bug that arrived around IntelliJ 2020.1.1 RC build # 201. Same behavior in final release of 2020.1.1.

See ticket # IDEA-237538, IntelliJ Build #IU-201.6668.121 no longer recognizes Java 14 records

Workaround: Add <configuration> elements

To make your Maven clean & install complete successfully, add <configuration> elements to two of your POM elements, to flag --enable-preview.

Change this:

            <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>

…to this:

            <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>


来源:https://stackoverflow.com/questions/61471575/records-preview-feature-in-intellij-2020-1-with-java-14-fails-with-compiler-er

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!