简体   繁体   English

Maven 不使用 JDK 11

[英]Maven Not Using JDK 11

I recently downloaded the JDK11 and the OpenJ9 JRE for JDK11.我最近为 JDK11 下载了 JDK11 和 OpenJ9 JRE。 I have set the Java classpath fine.我已经很好地设置了 Java 类路径。

When I run java -version command I get:当我运行java -version命令时,我得到:

openjdk version "11.0.10" 2021-01-19
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.10+9)
Eclipse OpenJ9 VM AdoptOpenJDK (build openj9-0.24.0, JRE 11 Windows 10 amd64-64-Bit Compressed References 20210120_899 (JIT enabled, AOT enabled)
OpenJ9   - 345e1b09e
OMR      - 741e94ea8
JCL      - 0a86953833 based on jdk-11.0.10+9)

But my Maven is still pointing to the old Java8 that I have installed.但是我的 Maven 仍然指向我安装的旧 Java8。 Running mvn -version gives me:运行mvn -version给我:

Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: H:\apache-maven-3.6.3\bin\..
Java version: 1.8.0_271, vendor: Oracle Corporation, runtime: C:\Program Files\Java\jdk1.8.0_271\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"

Hence when I compile a maven module I get the following error:因此,当我编译 maven 模块时,我收到以下错误:

Caused by: java.lang.IllegalArgumentException: invalid target release: 11

I have this set in my parent pom.xml:我在我的父母 pom.xml 中有这个设置:

<properties>

    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    
    <maven.compiler.target>11</maven.compiler.target>
    <maven.compiler.source>11</maven.compiler.source>

在此处输入图像描述

You can use the element inside maven-compiler-plugin.您可以使用 maven-compiler-plugin 中的元素。

As described here How to tell compiler to use openjdk 11 via pom.xml 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. As described here How to tell compiler to use openjdk 11 via pom.xml 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>

I use the toolchain mechanism of maven, works fine, you can switch easily of jdk compiler regarding which the project:我使用maven的工具链机制,工作正常,您可以轻松切换项目所涉及的jdk编译器:

    <toolchains xmlns="http://maven.apache.org/TOOLCHAINS/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="http://maven.apache.org/TOOLCHAINS/1.1.0 http://maven.apache.org/xsd/toolchains-1.1.0.xsd">
      <toolchain>
        <type>jdk</type>
        <provides>
          <version>1.8</version>
        </provides>
        <configuration>
          <jdkHome>your_java_8_local_path</jdkHome>
        </configuration>
      </toolchain>
      <toolchain>
        <type>jdk</type>
        <provides>
          <version>11</version>
        </provides>
        <configuration>
          <jdkHome>your_java_11_local_path</jdkHome>      
        </configuration>
      </toolchain>
    </toolchains>

Then just declare it in your pom.xml like this (compiling with jdk 11 by example here):然后像这样在你的 pom.xml 中声明它(这里用 jdk 11 编译):

<build>     
    <pluginManagement>
        <plugins>
            <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>
        </plugins>
    </pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-toolchains-plugin</artifactId>
            </plugin>
     </plugins>
</build>    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM