njk2015
njk2015

Reputation: 543

Maven compilation of my Java 8 source code fails

On my mac, I am trying to compile some Java 8 source code I wrote. It compiles fine in Eclipse, but in Maven, the compilation is failing.

I get multiple errors of the form:

[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /Users/admin/eclipse/workspaces/default/Java8/src/main/java/language/utilities/MapDemo.java:[14,33] lambda expressions are not supported in -source 1.7
  (use -source 8 or higher to enable lambda expressions)

Running mvn -v yields:

Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T16:41:47+00:00)
Maven home: /Users/admin/apache-maven-3.3.9
Java version: 1.8.0_91, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre
Default locale: en_IE, platform encoding: UTF-8
OS name: "mac os x", version: "10.12", arch: "x86_64", family: "mac"

Running the export command shows that the JAVA_HOME variable is set to:

JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home"

What is this '-source' property? Why do I need to set it if my Java version is Java 8?

Upvotes: 2

Views: 660

Answers (2)

Grim
Grim

Reputation: 1996

Set

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

in the properties of your pom.

You should not set this in the settings.xml because you force other people to change their settings.xml and use JDK8 in all of their other projects by default!

Upvotes: 4

Nicolas Filotto
Nicolas Filotto

Reputation: 44985

You need to configure your plugin maven-compiler-plugin to set the source and target version that you want to use

<plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
    </configuration>
</plugin>

Upvotes: 2

Related Questions