46

I use maven to build my project.

I have following configuration:

D:\freelance\polyndrom>mvn -verion Apache Maven 3.2.3 (33f8c3e1027c3ddde99d3cdebad2656a31e8fdf4; 2014-08-12T00:58:1 0+04:00) Maven home: C:\Program Files\apache\apache-maven-3.2.3 Java version: 1.8.0_25, vendor: Oracle Corporation Java home: C:\Program Files\Java\jdk1.8.0_25\jre Default locale: ru_RU, platform encoding: Cp1251 OS name: "windows 7", version: "6.1", arch: "amd64", family: "dos"

but when I compile project I see following errors:

lambda expressions are not supported in -source 1.5

I am confused - mven sees that I use java 8.

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>polyndrom</groupId>
    <artifactId>polyndrom</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.8.4</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>com.peterservice.polyndrom.Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>           
        </plugins>
    </build>
</project>
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710

3 Answers3

92

By default, Maven assumes you wrote your code using JDK 1.5 and that you want to compile to that same target. You will need to add the maven-compiler-plugin to your build plugins, in order to tell it to use 1.8.

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

Check out the plugin's docs for more info: http://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html

mattweyant
  • 1,058
  • 8
  • 6
15

You can specify language version in properties section in pom.xml file.

<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>
talex
  • 17,973
  • 3
  • 29
  • 66
  • 2
    much easier that defining compiler plugin – sandejai Aug 27 '19 at 04:25
  • Would be a better answer wiht link to supporting documentation, or even a bit more info about where `` are defined. is it in the gradle build file? Is it in AndroidStudio preferences? – JCutting8 Jun 06 '22 at 11:12
  • Fair enough. Extended my answer to add info about file that requires changes. – talex Jun 06 '22 at 11:29
2

I found possible solution for IntelliJ.

Go into Project settings (ctrl + shift + alt + s). Then you can change language level into higher version.

enter image description here

Kamil Naja
  • 6,267
  • 6
  • 33
  • 47
  • 2
    This won't work if you have maven based project. Idea will reread that setting from project eventionaly. – talex Aug 27 '19 at 04:33