0

I am using JDK 11 and my application has around 20 projects. Now I want my maven to pick JAVA_HOME instead of changing all pom.xml with maven-compiler-plugin or by properties. I have set JAVA_HOME to JDK 11 Output of mvn -v

OpenJDK 64-Bit Server VM warning: Ignoring option MaxPermSize; support was removed in 8.0
Apache Maven 3.0.5 (r01de14724cdef164cd33c7c8c2fe155faf9602da; 2013-02-19 19:21:28+0530)
Maven home: /home/krawler/apache-maven-3.0.5
Java version: 11.0.4, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-11-openjdk-11.0.4.11-0.el7_6.x86_64
Default locale: en_IN, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-957.1.3.el7.x86_64", arch: "amd64", family: "unix"

But when I run mvn clean install I get below error:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project mavenKwlCommonLibs: Compilation failure: Compilation failure:
[ERROR] error: Source option 5 is no longer supported. Use 6 or later.
[ERROR] error: Target option 1.5 is no longer supported. Use 1.6 or later.

kapil gupta
  • 335
  • 3
  • 19

1 Answers1

2

There is a difference between the JDK you use and the Java version you compile for. You can very well compile for Java 8 with a JDK 11 (just one example).

You need to set

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

in your POM to the Java version you want to build for (see also https://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html)

For Java 11, have also a look at https://stackoverflow.com/a/51586202/927493.

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
  • I am already able to do it by changing all pom.xml files of application this way, but I don't want to do it. I want my maven to pick JAVA_HOME set for both source and target or release as per maven-compiler-plugin 3.8.0 – kapil gupta Sep 05 '19 at 06:45
  • 1
    That cannot be done. We have a company parent POM to manage these versions. Alternatively, you can specify them on the command line. – J Fabian Meier Sep 05 '19 at 06:57
  • The Maven pom.xml is supposed to be self-contained. If it worked differently depending on environment variables, that would not work too well on other people's machines and you'd need to provide additional setup instructions. – Thilo Sep 05 '19 at 06:57
  • If you want to use maven-compiler-plugin:3.8.0, you have to specify that as well. From your error message, you are currently using 2.3.2. (But even 3.8.0 defaults to Java language level 1.6 and you are supposed to specify a higher version in your build) – Thilo Sep 05 '19 at 06:59
  • Maven has its quirks. Tedious but pays off in the long run. – Thorbjørn Ravn Andersen Sep 05 '19 at 08:40