161
votes

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?

10
Does your pom specify the source and target level in the compiler plugin config?Mark Rotteveel
yep, source & target set to 1.8. However it wasn't specified during the project importQuentin
@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.Thorbjørn Ravn Andersen

10 Answers

197
votes

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

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

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.

16
votes

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

9
votes

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.

4
votes

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.

1
votes

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

1
votes

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

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

0
votes

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.