How to change Java version for Maven in IntelliJ?

Summary:

  • 'maven-compiler-plugin' ALWAYS work! It is what I suggest you to use.

To change the language level, make usage of

<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.4</source>
                <target>1.4</target>
            </configuration>
        </plugin>
    </plugins>
</build>

The properties do not always change the language level of Intellij!

In the code below, 1.4 was configured in the pom using maven-compiler-plugin
(the jdk of Intellij is 1.8) and the language level of the project was changed accordingly to 1.4:

enter image description here

It was double-checked! It is an example. Most of the time you will not downgrade the version of the JDK to 1.4!

Of course if you use properties, let's say you put in the pom 1.8, then if you use a 1.8 JDK in Intellij(the language level default is 1.8 or the language default was changed manually), then you will be able to code in 1.8 BUT at the mvn compile, the properties will NOT be seen and you will default to Maven 1.5 and the compilation will NOT succeed !


Or easier, add this to your pom's properties section:

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

I don't think any response to the question addressed the concern - ". . . in IntelliJ".

Here are the steps: -

  • Go to Preference(or Settings) in IntelliJ ( or Shortcut on Mac ⌘ + ,)
  • Build, Execution, Deployment > Build Tools > Maven > Importing - select the "JDK for Importer" dropdown then select your preferred java version, Click Apply
  • Build, Execution, Deployment > Maven > Runner - select the "JRE" dropdown then select your preferred java version, Click Apply
  • Click OK

Change the source as shown below in pom.xml

<build>
        <finalName>MQService</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>