Maven编译时出现错误“不再支持源选项5。使用6或更高版本”

SUP*_*MAN 5 eclipse testing testng maven

我收到以下错误或$ mvn编译:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project Sym360: Compilation failure: Compilation failure: 
[ERROR] Source option 5 is no longer supported. Use 6 or later.
[ERROR] Target option 1.5 is no longer supported. Use 1.6 or later.
[ERROR] -> [Help 1]
Run Code Online (Sandbox Code Playgroud)

这是我的pom.xml的代码:

<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 
 http://maven.apache.org/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.test.app</groupId>
 <artifactId>Tset</artifactId>
 <packaging>jar</packaging>
 <version>1.0-SNAPSHOT</version>
 <name>Test</name>
 <url>http://maven.apache.org</url>

 <properties>
   <maven.compiler.source>6</maven.compiler.source>
   <maven.compiler.target>1.6</maven.compiler.target>
 </properties>

<build>
<pluginManagement>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>2.22.1</version>
    </plugin>
  </plugins>
</pluginManagement>
</build>
<dependencies>

<!-https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium- 
java -->
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.14.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>6.14.3</version>
    <scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)

我试图在pom.xml代码中添加属性,但仍然遇到相同的错误。有人可以帮我解决这个问题吗?提前致谢

小智 98

帮助我的是 pom.xml 文件中的这些行

<properties>
     <maven.compiler.source>1.8</maven.compiler.source>
     <maven.compiler.target>1.8</maven.compiler.target>
</properties>
Run Code Online (Sandbox Code Playgroud)

  • 如果您的目标是 jdk 11,请将 1.8 替换为例如 11 (13认同)
  • 我明白这是可行的,但我不明白为什么 maven 使用它不支持的默认源值? (12认同)
  • 我不久前就发现了这一点。原因是默认的 Maven 编译器插件非常旧,默认版本为“1.5”,这就是错误包含该版本的原因。如果您包含较新的编译器插件(自 3.0 起),默认值为 JDK 1.7,因此问题就会消失。例如,包含插件`org.apache.maven.plugins:maven-compiler-plugin:3.8.0`,默认为Java`1.7`(参见https://maven.apache.org/plugins/maven-compiler-plugin /) 并且错误消失。大多数人会想要“1.8”或“11”(或更高)。 (5认同)
  • @PatS`原因是默认的maven编译器插件非常旧,默认版本为1.5`好吧。但为什么 Maven 默认使用那个非常旧的插件呢?这是同样的问题,只是在不同的地方。为什么它在我昨天下载的 2022 年最新 Maven 3.8.6 版本上使用了一个损坏的、无法工作的 15 年前的插件或其他插件?这不好,我认为这会使整个包管理器崩溃。当我尝试使用准系统 .pom 文件和单个文件构建 HelloWorld Maven 项目时,我不应该出现构建错误。当我运行“mvn install”时,它应该可以正常工作并构建。 (4认同)

小智 30

我有同样的问题,问题出在属性上。检查您项目中的 JavaSE 版本,它将显示在您项目中的 JRE System Library 文件夹旁边。如果是 1.5,那么它会抛出一个错误。很可能你会有一个更新的版本,所以检查版本并更新它。我已经根据您的代码在下面更新了它。

<properties>
   <maven.compiler.source>1.6</maven.compiler.source>
   <maven.compiler.target>1.6</maven.compiler.target>
 </properties>
Run Code Online (Sandbox Code Playgroud)


Moh*_*asi 25

同样在我的一个项目中,除了上述所有答案之外,另一个尝试也有效:只需在项目结构的模块部分更改语言级别[下图] [语言水平] 1

  • 这就是一种无法解释的巫术,让你想转行 (5认同)

小智 14

我认为你的 pom.xml 错了:

<properties>
   <maven.compiler.source>6</maven.compiler.source>
   <maven.compiler.target>1.6</maven.compiler.target>
 </properties>
Run Code Online (Sandbox Code Playgroud)

改成:

<properties>
   <maven.compiler.source>1.6</maven.compiler.source>
   <maven.compiler.target>1.6</maven.compiler.target>
 </properties>
Run Code Online (Sandbox Code Playgroud)

现在取决于您是否使用命令行使用:

mvn 干净编译

或者任何一种方式(eclipse ide)

右键单击项目 Run with maven>build>Goal(编译)

  • [不正确]!!!源和目标版本“1.11”无效。必须是 11 而不是 1.11 (2认同)

小智 13

在pom中添加以下代码即可解决问题

      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
      </properties>
      <profiles>  
Run Code Online (Sandbox Code Playgroud)


小智 11

我有同样的问题,我在 pom.xml 中添加了以下配置并且它有效。

<build>
   <plugins>
   <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
        </configuration>
    </plugin>
   </plugins>
   </build>
Run Code Online (Sandbox Code Playgroud)


SUP*_*MAN 10

这帮助了我:

  1. 右键单击项目。
  2. 单击构建路径。
  3. 单击配置构建路径。
  4. 它会打开一个 Java 构建路径窗口。
  5. 单击左侧的 Java Compiler。
  6. 它导航到 Java Compiler 窗口,根据您的 jre 版本(例如,如果 java 版本为 1.8,则选择 1.8)作为选择来设置编译器合规性级别。
  7. 点击【应用】按钮。
  8. 点击【确定】按钮。
  9. 右键单击项目 > Maven > 更新项目。
  10. 右键单击 Project > Run As > Maven install -- pom.xml 文件正在运行,java jars 被下载并安装到项目中。
  11. 右键单击“项目”>“运行方式”>“Maven 测试”——pom.xml 文件正在运行,Java jar 已下载并安装到项目中。

然后您会收到 Build Success 消息,并且您的 Maven 项目已成功创建。

  • Eclipse 能够自动执行此操作。步骤 #9 应该是唯一要做的事情,假设 JDK 支持 1.8 (2认同)

Pat*_*atS 9

有人问:

\n
\n

我明白这是可行的,但我不明白为什么 maven 使用它不支持的默认源值?\xe2\x80\x93

\n
\n

答案是:

\n
    \n
  • Maven 不知道您将使用哪个版本的 JDK。

    \n
  • \n
  • 当与 JDK 1.8 一起使用时,Maven 编译工作成功。

    \n
  • \n
  • 当使用较新版本的 JDK 时,不清楚使用哪个版本的字节码。因此,Maven 已经放弃并继续使用它多年来使用的东西。

    \n
  • \n
\n

在当今时代,Maven(作为通用构建工具)很难选择每个人都会喜欢的默认字节码版本。

\n

因此,最好习惯将源代码的版本和要生成的字节代码放在 pom.xml 文件中。

\n

你可以争论(我同意)maven 应该(默认情况下)使用更新版本,maven-compiler-plugin但正如我所说,无论选择什么版本,都会有人遇到问题。

\n

例子

\n

例如,如果您使用 JDK 11,则-source 11 -target 11在编译代码时很可能会使用 Java 11 语法和需要。

\n

即使该插件的最新版本也maven-compiler-plugin:3.10.1默认使用 JDK 1.7 语法,这会导致 Java 11 代码出现编译错误。

\n

问题的完整描述

\n

其他人也这么说过,但要完整。默认的pom.xml没有指定maven-compiler-plugin。要找出所使用的版本,您可以使用

\n
mvn help:effective-pom | more\n      <plugin>\n        <artifactId>maven-compiler-plugin</artifactId>\n        <version>3.1</version>\n
Run Code Online (Sandbox Code Playgroud)\n

您将看到正在maven-compiler-plugin:3.1使用。\n您可以看到此版本生成一个命令,-target 1.5 -source 1.5当与高于 Java 1.8 的 Java 版本一起使用时,该命令会生成错误。

\n

下面显示了使用JDK 17时出现的错误。

\n
mvn --version\nApache Maven 3.8.6 (84538c9988a25aec085021c365c560670ad80f63)\nMaven home: D:\\p\\apache-maven-3.8.6\nJava version: 17.0.2, vendor: Oracle Corporation, runtime: D:\\p\\jdk-17.0.2\nDefault locale: en_US, platform encoding: Cp1252\nOS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"\n\nmvn clean install -X\n. . .\n[DEBUG] -d D:\\Play\\maven\\helloworld\\target\\classes -classpath D:\\Play\\maven\\helloworld\\target\\classes; -sourcepath D:\\Play\\maven\\helloworld\\src\\main\\java; -g -nowarn -target 1.5 -source 1.5\n. . .\n[ERROR] COMPILATION ERROR :\n[INFO] -------------------------------------------------------------\n[ERROR] Source option 5 is no longer supported. Use 7 or later.\n[ERROR] Target option 5 is no longer supported. Use 7 or later.\n[INFO] 2 errors\n
Run Code Online (Sandbox Code Playgroud)\n

修复

\n

修复方法是更新 maven pom.xml 文件以指定较新的maven-compiler-plugin. 我测试过maven-compiler-plugin:3.10.1并且它使用-target 1.7 -source 1.7

\n

语法是:

\n
  <build>\n    <plugins>\n      <plugin>\n        <groupId>org.apache.maven.plugins</groupId>\n        <artifactId>maven-compiler-plugin</artifactId>\n        <version>3.10.1</version>\n      </plugin>\n    </plugins>\n  </build>\n
Run Code Online (Sandbox Code Playgroud)\n

在撰写本文时,版本3.10.1是最新版本。

\n

另一种选择是指定要生成的字节码的版本,一种方法是使用它(如另一个答案中所述):

\n
<properties>\n     <maven.compiler.source>1.8</maven.compiler.source>\n     <maven.compiler.target>1.8</maven.compiler.target>\n</properties>\n
Run Code Online (Sandbox Code Playgroud)\n

即使使用旧版本 3.1 插件,这也可以工作。

\n


ahm*_*ira 6

我通过在pom.xml文件中添加以下内容来修复此问题:
<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>
    <failOnMissingWebXml>false</failOnMissingWebXml>
</properties>
Run Code Online (Sandbox Code Playgroud)


Sha*_*asi 5

我遇到了同样的问题,并用下面的代码行解决了它:

<properties>
   <maven.compiler.source>1.6</maven.compiler.source>
   <maven.compiler.target>1.6</maven.compiler.target>
 </properties>
Run Code Online (Sandbox Code Playgroud)