【问题标题】:Changing a 1.8 Maven Project to compile to 1.16 produces "invalid target release 1.16"将 1.8 Maven 项目更改为编译为 1.16 会产生“无效的目标版本 1.16”
【发布时间】:2021-10-09 00:14:25
【问题描述】:

我已经搜索了几个小时的解决方案,甚至找不到 1 篇关于 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

【问题讨论】:

  • 请查看mvn --version 显示的内容?如果不是我所期望的,应该有关于 JDK16 的输出信息。你必须更改 JAVA_HOME 环境变量以指向 JDK16 以使用 JDK 16 构建......
  • 如何从 Eclipse 运行这个命令?另外我已经把我的 JAVA_HOME 放到了 jdk 16... 没有工作
  • 不是来自 Eclipse 内部的普通命令行...实际上没有意义...
  • cmd 无法识别“mvn”...
  • 你当然必须安装 Maven 才能运行它......maven.apache.org/download.cgi 并安装它:maven.apache.org/install.html

标签: java maven build compilation upgrade


【解决方案1】:
  1. 不是1.16,是16
  2. 您不必配置maven-compiler-plugin
  3. 您可以使用the new release option
  <properties>
    <maven.compiler.release>16</maven.compiler.release>
  </properties>

【讨论】:

  • 即使在将版本和目标都更改为 16 后,我仍然会收到“致命错误编译:错误:无效目标版本:16 -> [帮助 1]”另外,我确实需要编译器插件,因为 java 版本不是我唯一设置的。
  • @David 你的 JDK 版本是什么?如果我在 cmd 行上的 JDK 11 上运行 javac -target 16 test.java,我也会得到 error: invalid target release: 16
  • 正如我在帖子中所写...我安装了 jdk 16,运行你的命令对我有用
  • @David 在 Eclipse 中选择的内容 → PreferencesJavaInstalled JREs ?如果您在 cmd 行而不是在 Eclipse 中构建会怎样?
  • prnt.sc/1isl20x 以及为什么但“mvn”在 cmd 中不起作用
【解决方案2】:

尽管我的设置是这样的,但我遇到了类似的问题

<source>16</source> //used to be 8
<target>16</target> //used to be 8

当我跑步时,真正的洞察力出现了

mvn --version

正如这篇文章指出的那样,我正在使用 JDK 11 运行当前的 maven。

解决方案是在运行 maven 之前将 JAVA_HOME 更改为指向我的 JDK 16 安装。

【讨论】:

    猜你喜欢
    • 2015-10-14
    • 2015-06-14
    • 2015-06-16
    • 2015-12-19
    • 2017-10-29
    • 2015-03-18
    • 2015-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多