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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53034953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:44:28  来源:igfitidea点击:

Error "Source option 5 is no longer supported. Use 6 or later" on Maven compile

eclipsemaventestingtestng

提问by SUPARNA SOMAN

I am getting the following error or $ mvn compile:

我收到以下错误或 $ mvn compile:

[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]

Here is the code of my pom.xml:

这是我的 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>

I tried to add properties in the pom.xml code, but still getting the same error. Can someone help me in resolving this issue? Thanks in advance

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

采纳答案by SUPARNA SOMAN

This helped me:

这帮助了我:

  1. Right Click on Project.
  2. Click on Build path.
  3. Click on Configure Build path.
  4. It opens a Java Build path window.
  5. Click on Java Compiler in the Left side.
  6. It navigates to Java Compiler window in that to set the Compiler compliance level is set as according to your jre version(ex if java version is 1.8 then choose 1.8) as select.
  7. Click on [Apply] button.
  8. Click on [OK] button.
  9. Right click on Project > Maven > Update the project.
  10. Right click on Project > Run As > Maven install -- The pom.xml file is running and java jars are download and installed to project.
  11. Right click on Project > Run As > Maven Test -- The pom.xml file is running and java jars are download and installed to project.
  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 已下载并安装到项目中。

Then you got the Build Success message and your maven project is created successfully.

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

回答by Mohsen Abasi

Also in one of my projects, in addition to all of answers above, another try works: Just change Language levelin Modules section of Project Structure[image below] [Language Level]1

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

回答by Sugat Shivsharan

I had same issue, the problem is with properties. Check your JavaSE version in your project, it will be displayed beside JRE System Library folder in your project. If it is 1.5, then it will throw an error. Most probably you will have a updated version, so check the version and update it. I have updated it below based on your code.

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

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

回答by eclat_soft

For the new Apache net Bean its a little bit different from the suggestion by SUPARNA SOMAN

对于新的 Apache net Bean,它与 SUPARNA SOMAN 的建议略有不同

  • Right Click on your Project -Hover on "set configuration" and click customize configuration-.A new dialogue box opens....
  • At the left corner where the categories are, Click on "Source"
  • At the select form on the page below, select your required version of JDK ----see image for this last step.the last step required to change jdk version
  • 右键单击您的项目 - 将鼠标悬停在“ set configuration”上,然后单击 -。customize configuration一个新的对话框打开....
  • 在类别所在的左角,单击“ Source
  • 在下面页面的选择表单中,选择您需要的 JDK 版本 ---- 最后一步请参见图片。更改 jdk 版本所需的最后一步

回答by José T.

I think you have wrong your pom.xml:

我认为你的 pom.xml 错了:

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

change to:

改成:

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

Now depending if you are using the command line use:

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

mvn clean compile

mvn 干净编译

or either way(eclipse ide)

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

Right click on project Run with maven>build>Goal (compile)

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

回答by Adelin

On MacOS I have multiple versions

在 MacOS 上我有多个版本

user> ls /Library/Java/JavaVirtualMachines/
jdk-11.0.4.jdk      jdk-12.0.2.jdk      jdk1.8.0_221.jdk

and JAVA_HOME was not defined properly so Maven used jdk-12. I have jdk-11,jdk-8, and jdk-12.

并且 JAVA_HOME 没有正确定义,所以 Maven 使用了 jdk-12。我有 jdk-11、jdk-8 和 jdk-12。

user> mvn -version
Apache Maven 3.6.1 
Maven home: /usr/local/Cellar/maven/3.6.1/libexec
Java version: 12.0.2, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk-12.0.2.jdk/Contents/Home
Default locale: XXX, platform encoding: UTF-8
OS name: "mac os x", version: "10.14.6", arch: "x86_64", family: "mac"

So:

所以:

Define JAVA_HOMEto use jdk-8.

定义JAVA_HOME使用 jdk-8。

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home

Try again, now maven is:

再试一次,现在maven是:

user> mvn -version
    Maven home: /usr/local/Cellar/maven/3.6.1/libexec
    Java version: 1.8.0_221, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/jre

and the build is:

构建是:

[INFO] BUILD SUCCESS

回答by Prashant Biradar

I had same issue and i have added below configuration in pom.xml and it works.

我有同样的问题,我在 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>

回答by kraftwerk

What helped me was these lines in pom.xml file

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

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