当我尝试在eclipse中编译(运行maven安装)时,出现了不兼容的错误。由于某种原因,看来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中指定的内容:
这是JAVA_HOME的环境。 var。
这是我的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也无济于事。
答案 0 :(得分:2)
您需要在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将使用非常老的版本进行构建。