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

JavaMavenIntellij Ideapom.xml

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

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

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

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

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

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.

Solution 6 - Java

None of the solutions helped in my case. I didn’t need to specify any Java version in my pom.xml.

I needed to open the <project-name>.iml file and change the JDK version there.

Original:

<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5">
    <!-- ...                                                   ^ -->
    <!-- ...                                                   | -->

Updated:

<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
    <!-- ...                                                   ^ -->
    <!-- ...                                                   | -->

This makes no sense at all. At no point have I specified a JDK version for Java 1.5.

Solution 7 - Java

I struggled a lot with this problem, due to building microservices with Dropwizard. Eventually I found out that I had my build properties in the wrong pom file (The main service's pom.xml).

So, even though the other packages are more like libraries, I were not able to use the Java 8 syntax.

When I refactored the build plugin into the "global" .pom.xml" file, all child containers were able to use the new syntax.

May help someone having issues with multi-container projects

Solution 8 - Java

thank you it works.

be careful not to make the same mistake I did.

if you have profiles, add the plugin in the right profile.

<profiles>
	<profile>
		<id>foo</id>
		<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.3.2</version>
				<configuration>
					<encoding>UTF-8</encoding>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
		</plugins>
		</build>
	</profile>
	<profile>
		<id>bar</id>
		.......
	</profile>
<profiles>

Solution 9 - Java

What solved the issue for me was a list of updates:

  • In Project Structure > modules: changes the language level Java version to 8
  • In Project Structure > Project: Java version should be 1.8
  • In pom file, same changes specified in the responses above
  • In Settings > Java compiler > changed the bytecode versions to 8
  • In Settings > maven > importing > JDK for importer should be 1.8

Solution 10 - Java

For me the solution of updating the POM (plugins plus properties) to the required Java compiler version (1_7 in my case) worked. However as the .iml file for each project was generated with original pom (with default compiler version of 1_5 as explained by someone above) has a JDK version of 1_5, this still overrides the pom version.

I deleted the .idea folder manually and imported the module into IntelliJ with a reimport from the updated pom. When I reimported the Module from updated POM,I could see that the iml files had the updated JDK version (1_7 in my case) .

Solution 11 - Java

There was one additional step I had to follow, in addition to setting the maven build properties, adding the maven-compiler-plugin, and modifying the Java version in the .iml file. (each documented already in the other answers). You also have to set the compiler version in the settings.

Solution 12 - Java

Problem: Expected Java version: 11 but Stuck at version: 8

  • I tried almost all the answers, still I was stuck with language_level 8 and nowhere in my project or in the Intellij IDE I found any trace that could relate to Java version 8.

  • Then I explored the pom.xml of the parent with no success but in the pom.xml of the grand-parent, I found that the release version is 8.

Solution: Adding the following 3 lines in the properties made the difference.

<properties>
  <maven.compiler.source>11</maven.compiler.source>
  <maven.compiler.target>11</maven.compiler.target>
  <maven.compiler.release>11</maven.compiler.release>
</properties>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionQuentinView Question on Stackoverflow
Solution 1 - JavavikingsteveView Answer on Stackoverflow
Solution 2 - JavaDieterDPView Answer on Stackoverflow
Solution 3 - JavadanwView Answer on Stackoverflow
Solution 4 - JavaPeter N. SteinmetzView Answer on Stackoverflow
Solution 5 - JavaAnuj TeotiaView Answer on Stackoverflow
Solution 6 - JavakleinfreundView Answer on Stackoverflow
Solution 7 - Javauser3677636View Answer on Stackoverflow
Solution 8 - Java936940View Answer on Stackoverflow
Solution 9 - Javauser666View Answer on Stackoverflow
Solution 10 - JavaShirish SakhareView Answer on Stackoverflow
Solution 11 - Javahosford42View Answer on Stackoverflow
Solution 12 - JavaNarnia_OptimusView Answer on Stackoverflow