108

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.

tearvisus
  • 2,013
  • 2
  • 15
  • 32

12 Answers12

131

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>
hamid
  • 1,828
  • 1
  • 13
  • 17
64

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
papigee
  • 6,401
  • 3
  • 29
  • 31
39

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 !

Rudy Vissers
  • 5,267
  • 4
  • 35
  • 37
  • Btw and I think this created the confusion, **Eclipse** does support maven.compiler.source and maven.compiler.target. – Rudy Vissers Jun 30 '16 at 08:12
  • I updated the pom file but I needed to change this in Project Settings, too, for it to wokr. – gloomy.penguin Jul 04 '16 at 03:39
  • @gloomy.penguin:I have just created a new Idea project from the Maven Quickstart archetype. Then I looked at Project Settings--->Modules and I read 5.0 - 'enum' keyword, generic, autoboxing etc. Then I have added the source (1.6) and target(1.6) from maven-compiler-plugin and did exactly the same and I read 6-@Override in interfaces. I think there is a confusion. WE DO NOT WANT TO CHANGE THE DEFAULT SDK. There is nothing we can do about that. – Rudy Vissers Jul 04 '16 at 10:11
  • @gloomy:penguin:If JDK 1.5 is configured by default and you change the config in the pom to ask 1.6 then Idea will not change the default SDK to 1.6 or 1.7 or 1.8 even if you have added these jdks to Intellij. The configuration in the pom ask Idea to change the source and target version using the default sdk! In the example in the picture 1.4 was asked and the default jdk is 1.8 and of course it is OK. You have to setup a default jdk that is compatible with the versions you configured into the pom. – Rudy Vissers Jul 04 '16 at 10:12
  • @gloomy-penguin:So 1.8 will support all the configurations (but not 9!!!). Hope it helps. I repeat I just checked and it works. And please check the result in Project Settings--->Modules. – Rudy Vissers Jul 04 '16 at 10:12
  • To see the change of configuration please have a look into Project Settings->Modules and click on the module name (like into the screen dump) and NOT Project Settings only. – Rudy Vissers Jul 11 '16 at 12:38
  • Wow - lots of comments. I was just saying what worked for me in case it helps someone else out ini the future. – gloomy.penguin Jul 11 '16 at 18:28
  • I'm not sure if it's because I am using intellij 2017.1.4, but if you use the properties and simply add the compiler plugin (you do not have to add a configuration with a hard coded value), it will change the project settings accordingly. – wrestang Jun 23 '17 at 01:01
  • @wrestang I have just done the test in 2017.1.4 and the properties didn't change the Intellij module version. Please do the following: Comment the configuration, source and target tags. Then assign 1.4 to the properties. Then check in Project settings and then modules. It is not 1.4. OK. Then uncomment the configuration and use 1.4 as source and target (it is a test). Then do the same. Check the version used in modules (wait some time). You will read '1.4 - assert keyword'. – Rudy Vissers Jun 23 '17 at 09:25
  • @wrestang Of course if it is configured in 1.4 then if you change the properties you will see that it will remain in 1.4! Nothing happened.Change the prop in 1.6. Nothing happened. 1.4 forever. Only the maven-compiler-plugin does the trick and the properties are not used by the plugin... If source and target are not used then it is 1.5 by default. – Rudy Vissers Jun 23 '17 at 09:31
  • Strange, it did work on mine, it defaults to 1.5 and was complaining about @Overrides tags, as soon as I put the compiler plugin with no configuration and set the properties, voila it no longer complained. The maven compiler plugin uses those settings so it is strange that when you specified the plugin it was not taking those settings into account for you, but was happy for me. – wrestang Jun 23 '17 at 13:23
  • @wrestang:Can you send me the Intellij project ? (compressed) @ rudy@rudyvissers.com ? I am curious. – Rudy Vissers Jun 25 '17 at 05:45
  • @Mart10 Indeed seems to work. I did it but today I have created a new Intellij project in 2017.2.5 and I am stuck in java 5 in making usage of the properties. Making usage of the configuration and source and target do work. I will correct the answer and suggest to use the maven-compiler-plugin + source and target. – Rudy Vissers Nov 20 '17 at 08:17
  • @RudyVissers whats weird is that i created a new maven project recently and the properties tag didnt work, so i switched to the plugins one. I was using the same installation of intellij – Mart10 Nov 21 '17 at 18:13
7

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>
Rahul
  • 3,479
  • 3
  • 16
  • 28
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

Community
  • 1
  • 1
Amit Kaneria
  • 5,466
  • 2
  • 35
  • 38
4

It may sometimes happen that after configuring maven in Intellij and changing as following it does not work by command build, so build it by Intellij maven tool.

Setting > Maven > Importer - select the `JDK` 
Setting > Maven > Runner - select the `JRE`

After that, try to build by Intellij maven tool instead of Intellij console.

Tohid Makari
  • 1,700
  • 3
  • 15
  • 29
1

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>
Anuj Teotia
  • 1,303
  • 1
  • 15
  • 21
1
Here are the steps for intellij version# 2021.2 Ultimate Edition: -

Go to Settings in IntelliJ
Build, Execution, Deployment > Build Tools > Maven > importing > "JDK for Importer" then select your preferred java version, Click Apply
Build, Execution, Deployment > Build Tools > Maven > Runner > For "JRE" option select your preferred java version, 
Click Apply
Click OK
user11160452
  • 43
  • 1
  • 9
0

You should add below code in your pom.xml to force the language level to change

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

now intelliJ 2019.3 CE does this for you if you go to import then alt+enter where you will get an option saying "change language level to 8 to use this functionality"

madroid
  • 336
  • 3
  • 12
0

open the ubuntu terminal goto your root directory and type:

export JAVA_HOME = <path to jdk>

for example, it works fine for me {do the same in IntelliJ terminal}.

export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64

to check the set value type echo $JAVA_HOME

to check the maven version type: mvn -version

you can find all path of JDKs by typing this command, and you can set JDK version.

sudo update-alternatives --config java

also check you have same java -version and javac -version.

0

You can click on the maven view in the extreme right, then Lifecycle -> right click on install and Modify Run Configuration:

1

Then expand Java Options, disable Inherit from settings and select your desired Java version.

2

0

If you don't want to modify the pom file and you want to make it general for all mvn runs without having to set it for a specific mvn command, you may want to update the settings for the Maven Runner:

enter image description here

Adel Helal
  • 642
  • 1
  • 9
  • 20