3

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

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • 1
    By any chance is there a way for you to ["(use --enable-preview to enable records)"](https://youtrack.jetbrains.com/issue/IDEA-237538)? – Elliott Frisch Apr 28 '20 at 02:23
  • @ElliottFrisch The bug in that ticket [*IDEA-237538*](https://youtrack.jetbrains.com/issue/IDEA-237538) does seem to be my problem. I added a comment on that page documenting my particulars. Thank you for providing that link. – Basil Bourque Apr 28 '20 at 02:37
  • 1
    I'm not planning to upgrade for a little bit yet. Hope they work the kinks out. – Elliott Frisch Apr 28 '20 at 02:40
  • @ElliottFrisch Oddly, my code runs. I can instantiate my `record` class, and call its methods. Only the Maven `install` throws this error. So for a simple little app just to play with `record`, the bug does not bite. Only for a complex app that requires a Maven `install` is a problem. – Basil Bourque Apr 28 '20 at 02:48
  • @ElliottFrisch If you make an Answer of your comment, I will accept it. – Basil Bourque Apr 28 '20 at 02:49
  • That's okay. I literally just got lucky on my search query. Feel free and self answer if you think it's worthwhile. I'm not using Java 14 yet (as I mentioned). – Elliott Frisch Apr 28 '20 at 02:53
  • Glad that I wasn't tempted by the balloon pop-up for the update. :) But did you not perform `mvn verify` from the terminal, just to compare the IDE to the command line? – Naman Apr 28 '20 at 05:10
  • @Naman No I did not. I do not know about Maven `verify`. – Basil Bourque Apr 28 '20 at 07:18
  • Related: [*IntelliJ IDEA 2020.1.1 (Ultimate Edition) can't detect JDK 14 record as reserved keyword*](https://stackoverflow.com/q/61925053/642706) – Basil Bourque May 21 '20 at 01:43

1 Answers1

4

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>
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • Based on the edit, related to [Compile and execute a JDK preview feature with Maven](https://stackoverflow.com/questions/52232681/compile-and-execute-a-jdk-preview-feature-with-maven) – Naman May 02 '20 at 00:18