首页 > 解决方案 > 将 1.8 Maven 项目更改为编译为 1.16 会产生“无效的目标版本 1.16”

问题描述

我一直在寻找解决方案几个小时,甚至找不到一篇关于 maven 编译到 1.16 的帖子

我最近决定将我的一个项目从 java 8 升级到 16,因此我安装了 16 个 JDK 并相应地更改了我的构建部分(在 pom.xml 中):

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>1.16</source> //used to be 1.8
                <target>1.16</target> //used to be 1.8
                <compilerArgs>
                    <arg>-parameters</arg>
                </compilerArgs>
            </configuration>
        </plugin>
    </plugins>
</build>

但是当我尝试安装时出现以下错误:

[INFO] Total time:  0.628 s
[INFO] Finished at: 2021-08-03T22:40:27+03:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project employeme: Fatal error compiling: error: invalid target release: 1.16 -> [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

标签: javamavenbuildcompilationupgrade

解决方案


  1. It's not 1.16, it's 16.
  2. You don't have to configure the maven-compiler-plugin.
  3. You can use the new release option.
  <properties>
    <maven.compiler.release>16</maven.compiler.release>
  </properties>

推荐阅读