2

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?

gtonic
  • 2,295
  • 1
  • 24
  • 32
njk2015
  • 543
  • 9
  • 20
  • 1
    The message shows somewhere in your poms something like `1.7` ...is done? – khmarbaise Oct 11 '16 at 18:54
  • check [here](http://stackoverflow.com/questions/16723533/how-do-you-specify-the-java-compiler-version-in-a-pom-xml-file) – Beloo Oct 11 '16 at 19:25

2 Answers2

4

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!

Grim
  • 1,938
  • 10
  • 56
  • 123
  • Thanks. I set this in the parent POM of my project? Can it not be set in 'settings.xml' perhaps? I want to set this property globally as I will be using Java 8 moving forward on my machine. – njk2015 Oct 11 '16 at 19:01
  • 1
    settings.xml is not absolute global, maybe you are looking for environment-variable like `MAVEN_OPTS="‑Dmaven.compiler.target=1.8 -Dmaven.compiler.source=1.8"` On this point Id like to referee http://stackoverflow.com/questions/9736777/how-to-configure-compiler-level-1-6-in-maven-settings-xml – Grim Oct 11 '16 at 19:04
  • 1
    The answer in this question has a well-voted comment to use the properties in the parent-pom and not in `settings.xml`. – Grim Oct 11 '16 at 19:10
2

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>
Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
  • 1
    The plug-in is active by default, it does not differ from my answer except that I set the user-property. – Grim Oct 11 '16 at 18:58
  • 2
    @PeterRader yes it is two ways to do the same thing, if the OP doesn't want to pollute his properties he can use this approach otherwise he can use yours – Nicolas Filotto Oct 11 '16 at 19:02
  • 1
    What do you think about his attempt to set it global? – Grim Oct 11 '16 at 19:08
  • 2
    @PeterRader I don't personally believe that it is a good idea because we usually have to change of version of Java from one project to another so it is better to keep it in the project pom or in a parent pom to keep full control but it is up to him now – Nicolas Filotto Oct 11 '16 at 19:12