Maven不使用Java 11:错误消息“致命错误编译:无效目标版本:11”

我试图用Java 11编译我的项目。

当我尝试使用Java 8作为 pom.xml 中的Java版本运行该应用程序时,它可以正常运行。但是,当我尝试使用Java 11运行它时,它会引发错误。

致命错误编译:无效目标发布:11

我尝试以各种方式修复它,例如更改环境变量,更新路径,并指向% Java_home %到Java 11。

我的计算机上的命令及其结果如下:

java -version
echo %JAVA_HOME%

输出:

java version "11.0.8" 2020-07-14 LTS
C:Program FilesJavajdk-11.0.8

我正在使用 IntelliJ IDEA ,并按照 this tutorial 中的建议进行了必要的更改。

我的 pom.xml 文件看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>com.google.cloud.tools</groupId>
                <artifactId>appengine-maven-plugin</artifactId>
                <version>2.2.0</version>
                <configuration>
                    <version>1</version>
                    <projectId>businessapplication-6963d</projectId>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>11</source>
                    <target>11</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

当我检查Maven版本时,它将JDK 1.8显示为Java版本:

mvn --version

输出:

Java version: 1.8.0_241, vendor: Oracle Corporation, runtime: C:Program FilesJavajdk1.8.0_241jre

我该如何指向Java 11?或者,如果不是问题,如何解决这个问题?我认为这个问题并不是 invalid target release: 1.7 的重复,因为我已经实现了该线程中提供的解决方案。

构建项目后的错误:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project demo: Fatal error compiling: invalid target release: 11 -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

路径环境变量是:

C:Program Files (x86)Common FilesIntelShared FilescppbinIntel64;
C:Program Files (x86)InteliCLS Client;
C:Program FilesInteliCLS Client;
C:WINDOWSsystem32;C:WINDOWS;
C:WINDOWSSystem32Wbem;
C:WINDOWSSystem32WindowsPowerShellv1.0;
C:Program Files (x86)IntelIntel(R) Management Engine ComponentsDAL;
C:Program FilesIntelIntel(R) Management Engine ComponentsDAL;
C:Program Files (x86)IntelIntel(R) Management Engine ComponentsIPT;
C:Program FilesIntelIntel(R) Management Engine ComponentsIPT;C:WINDOWSSystem32OpenSSH;
C:Delhi 2.0apache-maven-3.6.1bin;
C:Program FilesJavajdk-11.0.8bin;C:Program FilesIntelWiFibin;
C:Program FilesCommon FilesIntelWirelessCommon;
C:Program FilesGitcmd;D:Flutterflutterflutterbin;
C:UsersInfinity97AppDataLocalGoogleCloud SDKgoogle-cloud-sdkbin;
C:UsersInfinity97AppDataLocalMicrosoftWindowsApps;
C:Program FilesJetBrainsIntelliJ IDEA Community Edition 2019.2bin;
H:apache-maven-3.6.3bin;
C:Program FilesPostgreSQL12bin;
C:Program FilesPostgreSQL12lib;
C:Program FilesDocker Toolbox
stack overflow Maven is not using Java 11: error message "Fatal error compiling: invalid target release: 11"
原文答案

答案:

作者头像

最快,最简单的方法是使用SDKMAN https://sdkman.io/

1.使用以下方式安装JDK 11

 ```
sdk install 11.0.11.hs-adpt

``` 
*  `sdk list java` 对于可用JDK的列表

2.在项目主目录中创建一个.sdkmanrc文件:

 ```
java=11.0.11.hs-adpt

``` 

3.切换JDK

*使用 `sdk env` 进行切换到右JDK。

*对于自动切换到选定的JDK版本,请在SDKMAN配置文件中启用它( `~/.sdkman/etc/config` )

     ```
    sdkman_auto_env=true

    ```