9

I'm trying to set up a simple maven project with java 11. As I want to keep JAVA_HOME to be version 8, I'm using maven-toolchains-plugin to make maven use jdk11 for this project.

While maven successfully finds a matching toolchain for jdk-11.0.1, I keep getting " javac: invalid flag: --release". What am I doing wrong?

Here are the plugin configurations:

 <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.0</version>
    <configuration>
      <release>11</release>
    </configuration>
  </plugin>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-toolchains-plugin</artifactId>
    <version>1.1</version>
    <executions>
      <execution>
        <goals>
          <goal>toolchain</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <toolchains>
        <jdk>
          <version>11</version>
        </jdk>
      </toolchains>
    </configuration>
  </plugin>

The toolchain is defined as:

 <toolchain>
   <type>jdk</type>
   <provides>
     <version>11</version>
     <id>JavaSE-1.11</id>
   </provides>
   <configuration>
     <jdkHome>C:\Program Files\Java\jdk-11.0.1\bin</jdkHome>
   </configuration>
<toolchain>
cb4
  • 6,689
  • 7
  • 45
  • 57
Hengrui Jiang
  • 861
  • 1
  • 10
  • 23

2 Answers2

7

As I found out, the configuration is just fine. The problem was that jdkHome in toolchains.xml was pointing to the \jdk-11.0.1\bin direction instead of \jdk-11.0.1 directly..... Using <jdkHome>C:\Program Files\Java\jdk-11.0.1</jdkHome> solves the problem..

Hengrui Jiang
  • 861
  • 1
  • 10
  • 23
4

Changing the jdk version should fix the problem mostly. Replace

<version>1.11</version>

with

<version>11</version>

Do ensure though that your maven is configured with JDK-11 using the command mvn -version and confirming the Java version there. You can also verify the toolchains.xml JDK configured as well.


In case you're trying to compile using different versions of the compiler, you need to ensure executions under the maven-compiler-plugin as:

<executions>
    <execution>
        <id>java11</id>
        <phase>none</phase>
        <goals>
            <goal>compile</goal>
        </goals>
        <configuration>
            <release>11</release>
            <jdkToolchain>
                <version>11</version>
            </jdkToolchain>
            <compileSourceRoots>
                <compileSourceRoot>${project.basedir}/src/main/java11</compileSourceRoot>
            </compileSourceRoots>
            <outputDirectory>${project.build.outputDirectory}/META-INF/versions/11</outputDirectory>
        </configuration>
    </execution>
</executions>

Here is the sample pom.xml referred for the above.

Naman
  • 27,789
  • 26
  • 218
  • 353
  • I'm a bit confused. As you can see, I want to use a different jdk version (11) than java_home (8), which is the reason why I am using toolchain.xml to make maven use a different jdk for this project. Changing the version of the toolchain from 1.11 to 11 does not solve the problem. – Hengrui Jiang Dec 01 '18 at 15:07
  • @HengruiJiang Did you configure the `executions` in compiler plugin as well? – Naman Dec 01 '18 at 15:17
  • 1
    As I found out, the reason for failing was that I pointed jdkHome to the bin folder instead of directly to jdk.... *hideface* Thx for the help though! – Hengrui Jiang Dec 01 '18 at 15:26
  • @HengruiJiang I meant something similar with ... *Do ensure though that your maven is configured with JDK-11* – Naman Dec 01 '18 at 15:49