stop IntelliJ IDEA to switch java language level every time the pom is reloaded (or change the default project language level)
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.
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.
Related videos on Youtube
Quentin
Updated on July 08, 2022Comments
-
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 over 9 yearsDoes your pom specify the source and target level in the compiler plugin config?
-
Quentin over 9 yearsyep, source & target set to 1.8. However it wasn't specified during the project import
-
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 over 9 yearsthe plugin is present in the pom, wuth 1.8 target, but it doesn't change anything :(
-
vikingsteve over 9 yearsHave 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 over 9 yearsIndeed 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 over 9 yearsGreat, 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 about 9 yearsNop, 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 over 8 yearsOne 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 over 7 yearsAgreed. 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 over 6 yearsOne annoyance less!
-
user26270 about 6 yearsI'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 over 5 yearsIn 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 over 5 yearsThis 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 about 5 yearsin IntelliJ, remember to re-import maven settings after the modification. The solution worked for me.
-
Hasan Aslam over 4 yearsResolved my issue. Seems like IntelliJ updated their internal JRE with the later releases (somewhere around 2019.2). Pretty tough to track down...
-
Michael Christoff about 4 yearsThis, 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 about 4 yearsExample: <... LANGUAGE_LEVEL="JDK_11">
-
kevinarpe almost 3 yearsIf 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 over 2 yearsThank 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 almost 2 yearsThanks! Changing the Project SDK and Module SDK in Project Structure > Project Settings worked for me.