stop IntelliJ IDEA to switch java language level every time the pom is reloaded (or change the default project language level)

89,643

Solution 1

As per Mark's comment, here is how to do it:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

Solution 2

A shorter version of vikingsteve's answer is:

<properties>
  <maven.compiler.source>1.8</maven.compiler.source>
  <maven.compiler.target>1.8</maven.compiler.target>
</properties>

Solution 3

I'm upgrading a project from JDK 8 to JDK 10+. I had the compiler properties specified correctly as follows:

<properties>
  <maven.compiler.source>10</maven.compiler.source>
  <maven.compiler.target>10</maven.compiler.target>
</properties>

However the Idea project would keep resetting the language level to 8.

Eventually I figured out that Idea's Maven import process was using JDK 8 to import the project which limited the language level to <= 8.

To fix I updated the 'JDK for importer' property under Settings -> Build, Execution, Deployment -> Build Tools -> Maven -> Importing to use JDK 11.

enter image description here

Solution 4

I think this has to do with a conceptual conflict between the Maven compiler plugin and IntelliJ idea. Apparently the newer versions of the compiler plugin have a default level of 1.5 (see http://maven.apache.org/plugins/maven-compiler-plugin/). So if the compiler plugin is used at all in a project, and the compiler level is not explicitly set in the pom.xml, whenever the POM is re-processed the level will revert to the default.

So there is a conceptual conflict which is ignored by Intellij IDEA. The IDE still allows one to set the project and module settings, but provides no warning or feedback that this setting is controlled by pom.xml. Solutions would either be to explicitly allow overriding the POM compiler plugin setting (perhaps not wise because what then happens when you use maven on the command line), or to deactivate the controls in the IDE when this setting from the POM is in effect.

The solution at the present time is to set the desired compiler level in the compiler plugin in the pom, the re-import, rather than trying to set it in module settings.

Solution 5

There are two ways of doing this, add either one of them in your pom.xml file:

First- Add Properties

<properties>
  <maven.compiler.source>1.8</maven.compiler.source>
  <maven.compiler.target>1.8</maven.compiler.target>
</properties>

second- Add Plugin

<plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
        </configuration>
</plugin>

Let me know if it helps.

Share:
89,643

Related videos on Youtube

Quentin
Author by

Quentin

Updated on July 08, 2022

Comments

  • Quentin
    Quentin almost 2 years

    Using IntelliJ 12, I have a java project and I use maven with a pom.xml. My project is using java8, but it seems the default project language level has been set to 6 while importing the project.

    I can change the language level to 8.0 (F4 -> Modules -> Language level) however every time I edit my pom.xml the project level is switched back to "use project language level", and I have to edit this settings again and again.

    Is there something I need to add to the pom.xml to set the default language level to 8.0?

    • Mark Rotteveel
      Mark Rotteveel over 9 years
      Does your pom specify the source and target level in the compiler plugin config?
    • Quentin
      Quentin over 9 years
      yep, source & target set to 1.8. However it wasn't specified during the project import
    • matbrgz
      matbrgz over 5 years
      @Quentin In such a situation it may be the easiest to reclone the project in a new location and let IntelliJ import that instead anew.
  • Quentin
    Quentin over 9 years
    the plugin is present in the pom, wuth 1.8 target, but it doesn't change anything :(
  • vikingsteve
    vikingsteve over 9 years
    Have you got auto-import of your maven pom's enabled? Have you checked the parent (and grandparent) poms, is there any 1.6 settings there that might be conflicting?
  • Quentin
    Quentin over 9 years
    Indeed the parent project is defined with spring-boot-starter-parent-1.1.9.RELEASE.pom.xml and in pluginManagement the maven-compiler-plugin is set to 1.6... I tried to override this settings without any success. Looks like spring boot 1.2.RC doesn't include maven-compiler-plugin anymore, I'll give it a try. - auto import is enabled.
  • vikingsteve
    vikingsteve over 9 years
    Great, that could be why. If you specify same version of maven compiler plugin, it might be possible to override the source / target settings. Perhaps try that in your project root pom.xml.
  • Quentin
    Quentin about 9 years
    Nop, I've upgraded the pom to spring boot 1.2.1.RELEASE, still in java 1.6 level when I re-import the pom :'(
  • ach
    ach over 8 years
    One gotcha with this is that IntelliJ will still show your Project Language level as something else, but it should set the Module Language level correctly.
  • Gillfish
    Gillfish over 7 years
    Agreed. The lack of feedback that the settings in IDEA are just going to be ignored and re-imported from the POM is very confusing, especially when using a parent POM.
  • Artur Cichosz
    Artur Cichosz over 6 years
    One annoyance less!
  • user26270
    user26270 about 6 years
    I've done both of these and the compiler is still being reset to 1.5 every time I try to run mvn compile from within IntelliJ. However, it works fine when run from the command line outside of IntelliJ.
  • genonymous
    genonymous over 5 years
    In my case this did not work. I had to remove the setting mentioned in this answer and instead add the following: <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties>
  • genonymous
    genonymous over 5 years
    This wasn't just a short version but a necessary one in my case. vikingsteve's answer did not work for me. So I removed that setting and used this one.
  • Pierluigi Vernetto
    Pierluigi Vernetto about 5 years
    in IntelliJ, remember to re-import maven settings after the modification. The solution worked for me.
  • Hasan Aslam
    Hasan Aslam over 4 years
    Resolved my issue. Seems like IntelliJ updated their internal JRE with the later releases (somewhere around 2019.2). Pretty tough to track down...
  • Michael Christoff
    Michael Christoff about 4 years
    This, by itself, solved the issue for me in IntelliJ 2019.3, without requiring that I change anything in my pom's or anywhere else. I similarly never specified JDK 1.5 anywhere.
  • Daniel De León
    Daniel De León about 4 years
    Example: <... LANGUAGE_LEVEL="JDK_11">
  • kevinarpe
    kevinarpe almost 3 years
    If nothing else works, try to close the project, exit IntelliJ, delete your project ".idea" folder and all .iml files, restart IntelliJ and open the root pom.xml again. IntelliJ will prompt open pom.xml as a for or open as a project? Open as a project. This will force IntelliJ to recreate .idea folder and all .iml files. I work on huge enterprise legacy Java projects with 100s of dependencies. IntelliJ frequently freaks and nothing fixes it except delete project and re-create... but it does seem to work!
  • LittletonDoug
    LittletonDoug over 2 years
    Thank you! I had tried all the other suggestions without luck. This was the missing piece and I wouldn't have found it on my own.
  • Babad00k
    Babad00k almost 2 years
    Thanks! Changing the Project SDK and Module SDK in Project Structure > Project Settings worked for me.