java - Maven不使用本地JRE/JDK

标签 java maven

当我尝试在 Eclipse 中编译(运行 maven install)时,出现不兼容的错误。似乎由于某种原因,maven 使用旧的 jre/jdk 并且不使用在路径环境变量。即使当我运行 mvn -v 时,它输出 maven 使用正确的版本(在我的本地计算机上)

[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /C:/Users/Barracuda/Desktop/Projects/Eclipse Projects/PetClinicWeb/src/main/java/com/model/databasePackage/implementations/DBConnection.java:[132,21] try-with-resources is not supported in -source 1.5
  (use -source 7 or higher to enable try-with-resources)
[ERROR] /C:/Users/Barracuda/Desktop/Projects/Eclipse Projects/PetClinicWeb/src/main/java/com/model/databasePackage/implementations/DBConnection.java:[223,50] diamond operator is not supported in -source 1.5
  (use -source 7 or higher to enable diamond operator)
[ERROR] /C:/Users/Barracuda/Desktop/Projects/Eclipse Projects/PetClinicWeb/src/main/java/com/model/adv/DBConnection.java:[64,38] diamond operator is not supported in -source 1.5
  (use -source 7 or higher to enable diamond operator)
[ERROR] /C:/Users/Barracuda/Desktop/Projects/Eclipse Projects/PetClinicWeb/src/main/java/com/model/adv/DBConnection.java:[80,21] try-with-resources is not supported in -source 1.5
  (use -source 7 or higher to enable try-with-resources)
[ERROR] /C:/Users/Barracuda/Desktop/Projects/Eclipse Projects/PetClinicWeb/src/main/java/com/model/databasePackage/implementations/ObjectComparators.java:[21,48] diamond operator is not supported in -source 1.5
  (use -source 7 or higher to enable diamond operator)
[ERROR] /C:/Users/Barracuda/Desktop/Projects/Eclipse Projects/PetClinicWeb/src/main/java/com/model/adv/ObjectComparators.java:[21,48] diamond operator is not supported in -source 1.5
  (use -source 7 or higher to enable diamond operator)
[ERROR] /C:/Users/Barracuda/Desktop/Projects/Eclipse Projects/PetClinicWeb/src/main/java/com/model/adv/DatabaseTable.java:[420,51] diamond operator is not supported in -source 1.5
  (use -source 7 or higher to enable diamond operator)
[INFO] 7 errors 
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.142 s
[INFO] Finished at: 2019-04-14T09:44:36+03:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project PetClinic: Compilation failure: Compilation failure: 
[ERROR] /C:/Users/Barracuda/Desktop/Projects/Eclipse Projects/PetClinicWeb/src/main/java/com/model/databasePackage/implementations/DBConnection.java:[132,21] try-with-resources is not supported in -source 1.5
[ERROR]   (use -source 7 or higher to enable try-with-resources)
[ERROR] /C:/Users/Barracuda/Desktop/Projects/Eclipse Projects/PetClinicWeb/src/main/java/com/model/databasePackage/implementations/DBConnection.java:[223,50] diamond operator is not supported in -source 1.5
[ERROR]   (use -source 7 or higher to enable diamond operator)
[ERROR] /C:/Users/Barracuda/Desktop/Projects/Eclipse Projects/PetClinicWeb/src/main/java/com/model/adv/DBConnection.java:[64,38] diamond operator is not supported in -source 1.5
[ERROR]   (use -source 7 or higher to enable diamond operator)
[ERROR] /C:/Users/Barracuda/Desktop/Projects/Eclipse Projects/PetClinicWeb/src/main/java/com/model/adv/DBConnection.java:[80,21] try-with-resources is not supported in -source 1.5
[ERROR]   (use -source 7 or higher to enable try-with-resources)
[ERROR] /C:/Users/Barracuda/Desktop/Projects/Eclipse Projects/PetClinicWeb/src/main/java/com/model/databasePackage/implementations/ObjectComparators.java:[21,48] diamond operator is not supported in -source 1.5
[ERROR]   (use -source 7 or higher to enable diamond operator)
[ERROR] /C:/Users/Barracuda/Desktop/Projects/Eclipse Projects/PetClinicWeb/src/main/java/com/model/adv/ObjectComparators.java:[21,48] diamond operator is not supported in -source 1.5
[ERROR]   (use -source 7 or higher to enable diamond operator)
[ERROR] /C:/Users/Barracuda/Desktop/Projects/Eclipse Projects/PetClinicWeb/src/main/java/com/model/adv/DatabaseTable.java:[420,51] diamond operator is not supported in -source 1.5
[ERROR]   (use -source 7 or higher to enable diamond operator)
[ERROR] -> [Help 1]
[ERROR] 

我应该如何解决这个问题?我可以在不每次都在 POM.xml 中指定要使用哪个版本的情况下做到这一点,而是告诉 maven 使用 Path env。变种? 这是 eclipse 中指定的内容:

enter image description here

这是 JAVA_HOME 环境。变种。

enter image description here

这是 MAVEN_HOMEenv。变种 enter image description here

这里 — mvn -v 和 java -version 的输出 enter image description here

enter image description here

这是 PATH 环境。变量: 。 enter image description here

这是我的 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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.arslan</groupId>
    <artifactId>PetClinic</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>Pet Clinic Web Application</name>

    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

    </dependencies>
</project>

即使从命令行运行 mvn install 也没有帮助。

最佳答案

您需要在 POM 中设置以下内容:

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

原因如下:即使使用正确配置的 JDK 1.8,您也可以使用不同的兼容性级别(1.7、1.6,...)进行构建。如果不给出版本,Maven 将使用非常旧的版本进行构建。

关于java - Maven不使用本地JRE/JDK,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55672785/

相关文章:

java - Java中的BPSK调制

java - 在 testng @AfterMethod 中检测测试失败

maven - 即使我所有的测试都通过了,我还是看到了 surefire 错误的错误,没有与 surefire 插件相关的答案帮助我

java - 如何让Spring Boot项目通过wall包运行,也可以通过jar运行

java - Spring ApplicationContext 在 bean 调用之前关闭

java - 将 maven-bundle-plugin 包含的资源声明为可选

java - 关于 ThreadGroup#activeCount() 的困惑

java - Java 中是否有不产生垃圾的 HashMap 实现?

java - Volley 库在发布请求时给出错误 500

java - TestNG 继续执行测试类,即使之前的类失败了