How to change Java version for Maven in IntelliJ?

2019-01-13 17:43发布

问题:

I'm new to both Maven and IntelliJ IDEA.

I have a Maven project written in Java 8. Whenever I try to build it (Maven Projects window -> Lifecycle -> compile -> Run Maven Build) I get a series of compilation errors:

[ERROR] path/to/file.java:[26,52] lambda expressions are not supported in -source 1.5
(use -source 8 or higher to enable lambda expressions)

Where am I supposed to change the value of the -source parameter? I tried adding it as an additional parameter in Settings -> Compiler - > Java Compiler, but I got the same results.

The project's and module's language levels are both set to 8.0.

I'm using Maven 3.2.3 and IntelliJ IDEA Community Edition 13.1.2.

回答1:

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>


回答2:

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:

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 !



回答3:

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>


回答4:

Adding below lines to root(project level) pom.xml worked me to resolve above issue: (both of the options worked for me)

Option 1:

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

Option 2:

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

originally posted at: IntelliJ IDEA 13 uses Java 1.5 despite setting to 1.7



回答5:

There are two ways of doing this :

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>