4

How to configure OpenJDK 11 in pom file.

<properties>
  <maven.compiler.plugin.version>3.8.0</maven.compiler.plugin.version>
  <maven.compiler.target>11</maven.compiler.target>
  <maven.compiler.source>11</maven.compiler.source>
</properties>
Alexander Petrov
  • 9,204
  • 31
  • 70
R K
  • 43
  • 1
  • 1
  • 3
  • 4
    You can't configure *OpenJDK 11*, you can configure maven to use java11, whether it will use Oracle Java or OpenJDK depends on which of the two is installed in your system and configured in your ide (or path, if you are using CLI). https://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html – BackSlash Jun 18 '19 at 07:18
  • You can't set it in the `pom.xml`. If you are using any IDE for building maven project change the JDK version you like to use. – Ramesh Subramanian Jun 18 '19 at 07:40
  • Thank you for response But if I configure as 11 in pom means getting the error. [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project pangolin: Fatal error compiling: invalid target release: 11 -> [Help 1] – R K Jun 18 '19 at 09:02

2 Answers2

3

The problem is probably in the version of Java you're using to run Maven. See the discussion here:

Unable to compile simple Java 10 / Java 11 project with Maven

If you want to make Maven recognize a target release of 10 or 11, you have to first run Maven with Java 11. Check that the Java that Maven is using is correct by doing mvn --version.

Piotr Wilkin
  • 3,446
  • 10
  • 18
3

Here is a bit more advanced example that allows you to compile Java 11 while your main java version is 8. Most of my projects are still java 8 so I find it usefull.

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
            <configuration>
                <release>11</release>
                <fork>true</fork>
                <executable>C:\Program Files\Java\jdk-11.0.2\bin\javac</executable>
            </configuration>
        </plugin>
Alexander Petrov
  • 9,204
  • 31
  • 70
  • this setup doesn't work for me. I have the same (main java 8 with a single submodule in 11). The compiler plugin just fails with no error and if I try to re-run the command that the plugin fires it actually works – alexlipa Feb 16 '20 at 12:04
  • 1
    this was the only thing that worked for me, every other suggestion failed when as soon as i hit the reimport button on maven, the iml file would switch back. thanks so much – Pix81 Jul 22 '20 at 20:37