Java 17 和 maven-archetype-quickstart 给出错误:不再支持 Source Option 5

发布于 2025-01-11 08:28:00 字数 2265 浏览 0 评论 0原文

当使用maven命令创建一个简单的Maven项目(使用最新版本的maven和java)时,我收到错误:

[ERROR] Source option 5 is no longer supported. Use 7 or later.
[ERROR] Target option 5 is no longer supported. Use 7 or later.

那么,需要进行什么更改来修复此问题。我是否更改maven命令或者之后编辑 pom.xml 文件?

要重现错误

首先验证您正在运行的 Java 和 Maven 版本。 在提出这个问题时,我正在使用最新版本的 Maven 和 Java。 Maven:3.8.4 和 Java 17.0.2
下面详细介绍

mvn --version
Apache Maven 3.8.4 (9b656c72d54e5bacbed989b64718c159fe39b537)
Maven home: D:\p\apache-maven-3.8.4
Java version: 17.0.2, vendor: Oracle Corporation, runtime: D:\p\jdk-17.0.2
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"

java -version
java version "17.0.2" 2022-01-18 LTS
Java(TM) SE Runtime Environment (build 17.0.2+8-LTS-86)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.2+8-LTS-86, mixed mode, sharing)

创建类似maven hello world的程序

我使用这个命令创建了Java maven示例程序。

mvn archetype:generate -DgroupId=com.example -DartifactId=helloworld -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

我发现以下链接更详细地解释了这一点(如果需要)。 https://facetingissuesonit.com/2017 /06/06/如何创建-maven-java-console-project/

构建代码

接下来,构建代码并观察错误。

cd helloworld
mvn clean install
[ERROR] COMPILATION ERROR :
[ERROR] Source option 5 is no longer supported. Use 7 or later.
[ERROR] Target option 5 is no longer supported. Use 7 or later.

请注意错误

但是,如果我切换回 Java 8 (JDK 1.8) 并运行 maven build 命令,则代码会成功编译。下面是成功的编译以及使用的 maven 版本和 JDK 版本,结果是编译成功。

mvn clean install
[INFO] BUILD SUCCESS

mvn --version
Apache Maven 3.8.4 (9b656c72d54e5bacbed989b64718c159fe39b537)
Maven home: D:\p\apache-maven-3.8.4
Java version: 1.8.0_271, vendor: Oracle Corporation, runtime: D:\p\jdk1.8.0_271\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"

我的问题是,如何解决此问题?以及为什么会发生这种情况?

When using the maven command to create a simple Maven project (with the latest versions of maven and java), I get the error:

[ERROR] Source option 5 is no longer supported. Use 7 or later.
[ERROR] Target option 5 is no longer supported. Use 7 or later.

So, what change is needed to fix this. Do I change the maven command or edit the pom.xml file afterwards?

To reproduce the error

First verify what versions of Java and Maven you are running.
At the time of this question, I'm using the latest versions of Maven and Java.
Maven: 3.8.4 and Java 17.0.2
Below are details

mvn --version
Apache Maven 3.8.4 (9b656c72d54e5bacbed989b64718c159fe39b537)
Maven home: D:\p\apache-maven-3.8.4
Java version: 17.0.2, vendor: Oracle Corporation, runtime: D:\p\jdk-17.0.2
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"

java -version
java version "17.0.2" 2022-01-18 LTS
Java(TM) SE Runtime Environment (build 17.0.2+8-LTS-86)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.2+8-LTS-86, mixed mode, sharing)

Create the maven hello world-like program

I used this command to create the Java maven sample program.

mvn archetype:generate -DgroupId=com.example -DartifactId=helloworld -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

I found the following link which explains this in more detail (if needed).
https://facingissuesonit.com/2017/06/06/how-to-create-maven-java-console-project/.

Build the code

Next, build the code and observe the error.

cd helloworld
mvn clean install
[ERROR] COMPILATION ERROR :
[ERROR] Source option 5 is no longer supported. Use 7 or later.
[ERROR] Target option 5 is no longer supported. Use 7 or later.

Notice the ERRORs.

But, if I switch back to Java 8 (JDK 1.8) and run the maven build command, then the code compiles successfully. Below is the successful compile and the maven version and JDK version used that result in a successful compilation.

mvn clean install
[INFO] BUILD SUCCESS

mvn --version
Apache Maven 3.8.4 (9b656c72d54e5bacbed989b64718c159fe39b537)
Maven home: D:\p\apache-maven-3.8.4
Java version: 1.8.0_271, vendor: Oracle Corporation, runtime: D:\p\jdk1.8.0_271\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"

My question is, how do I fix this? and Why does this happen?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

披肩女神 2025-01-18 08:28:01

出现此问题的原因是 maven 插件 (maven-compiler-plugin:3.1) 将参数 -source 1.5 -target 1.5 传递给 JDK 17 java 编译器。

JDK 17 编译器不支持为 Java 1.5 运行时生成代码,因此会产生如下错误:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile 
[ERROR] Source option 5 is no longer supported. Use 7 or later.
[ERROR] Target option 5 is no longer supported. Use 7 or later.

如果在运行 maven 命令时使用 -X 调试标志,您可以看到此错误,如下所示:

mvn -X clean install
. . . lots of output omitted . . .
[DEBUG] Command line options:
[DEBUG] -target 1.5 -source 1.5 -d target\classes -classpath target\classes; -sourcepath src\main\java; -g -nowarn
. . .

修复方法是修改 pom.xml 文件,以便 maven-compiler-plugin 传递诸如 -source 1.8 -target 1.8 (或更高的数字)之类的选项那JDK 17 可以生成针对 JDK 17 编译器支持的 JDK 版本的代码。

例如,要使用 JDK 1.8,您需要将以下内容添加到您的 pom.xml 文件中。

<properties>
     <maven.compiler.source>1.8</maven.compiler.source>
     <maven.compiler.target>1.8</maven.compiler.target>
</properties>

如果您是,问题和答案 https://stackoverflow.com/a/60344341/3281336 也可能会添加更多信息感兴趣的。

注意:maven-compiler-plugin 的 3.1 版本非常旧(2013 年 4 月发布),基于 https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin 所以当时,Java 1.5 版本是一个可行的选择。

The problem occurs because the maven plugin (maven-compiler-plugin:3.1) passes the arguments -source 1.5 -target 1.5 to the JDK 17 java compiler.

The JDK 17 compiler doesn't support generating code for the Java 1.5 runtime so the error is produced as shown:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile 
[ERROR] Source option 5 is no longer supported. Use 7 or later.
[ERROR] Target option 5 is no longer supported. Use 7 or later.

You can see this if you use the -X debug flag when running the maven command as shown below:

mvn -X clean install
. . . lots of output omitted . . .
[DEBUG] Command line options:
[DEBUG] -target 1.5 -source 1.5 -d target\classes -classpath target\classes; -sourcepath src\main\java; -g -nowarn
. . .

The fix is to modify the pom.xml file so that the maven-compiler-plugin will pass options like -source 1.8 -target 1.8 (or some higher number) so that the JDK 17 can produce code that is targeted for a version of the JDK that the JDK 17 compiler supports.

For example, to use JDK 1.8, you would add the following to your pom.xml file.

<properties>
     <maven.compiler.source>1.8</maven.compiler.source>
     <maven.compiler.target>1.8</maven.compiler.target>
</properties>

The question and answer https://stackoverflow.com/a/60344341/3281336 also might add some more information if you are interested.

NOTE: The 3.1 version of the maven-compiler-plugin is very old (released Apr 2013) based on https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin so back then, Java version 1.5 was a viable choice.

蝶…霜飞 2025-01-18 08:28:01

这对我有用。

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!-- <maven.compiler.source>1.8</maven.compiler.source> -->
        <!-- <maven.compiler.target>1.8</maven.compiler.target> --> 
        <maven.compiler.release>17</maven.compiler.release>
    </properties>

It worked for me.

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!-- <maven.compiler.source>1.8</maven.compiler.source> -->
        <!-- <maven.compiler.target>1.8</maven.compiler.target> --> 
        <maven.compiler.release>17</maven.compiler.release>
    </properties>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文