Maven Compilation Error: (use -source 7 or higher to enable diamond operator)
Maven Compilation Error: (use -source 7 or higher to enable diamond operator)
我在 IntelliJ、JDK1.8、maven 3.2.5 中使用 maven。出现编译错误:使用 -source 7 或更高版本启用 diamond opera。详情如下:
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] TrainingConstructor.java:[31,55] diamond operator is not supported in -source 1.5 (use -source 7 or higher to enable diamond operator)
[ERROR] DTM.java:[79,21] try-with-resources is not supported in -source 1.5 (use -source 7 or higher to enable try-with-resources)
[ERROR] ticons.java:[53,44] diamond operator is not supported in -source 1.5 (use -source 7 or higher to enable diamond operator)
有什么建议吗?是否有任何其他配置来设置此 -source 级别?似乎它不使用 java 1.8。
检查您的 maven-compiler-plugin
是如何配置的,它应该使用 java 版本 7 或更高版本:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
要获得更完整的答案,请参阅 。
您必须更改配置:
<project>
[...]
<build>
[...]
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
[...]
</build>
[...]
</project>
你应该了解JavaC中的source/taget
选项和JDK1.8/1.7等的用法的区别
除此之外,您应该升级使用 maven-compiler-plugin。
如果您已经尝试过@Sergey Pauk 和@khmarbaise 解决方案,请查看设置 -> 构建、执行、部署 -> 编译器 -> Java 编译器,有目标字节码版本特定模块
解决方案 1 - 在 pom.xml
中设置这些属性
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
解决方案 2 - 配置 Maven 编译器插件(始终在 pom.xml 中)
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
...
为什么会这样
出现问题是因为
[...] at present the default source setting is 1.5 and the default target setting is 1.5, independently of the JDK you run Maven with. If you want to change these defaults, you should set source and target as described in Setting the -source and -target of the Java Compiler.
以及最近的 Maven 版本:
Also note that at present the default source setting is 1.6 and the default target setting is 1.6, independently of the JDK you run Maven with. You are highly encouraged to change these defaults by setting source and target as described in Setting the -source and -target of the Java Compiler.
这就是为什么更改 JDK 对源级别没有影响。因此,您有几种方法可以告诉 Maven 使用哪个源代码级别。
JDK 要使用的版本?
如果像本例中那样设置目标 1.7,请确保 mvn 命令实际上是使用 jdk7(或更高版本)启动的
语言水平 IDE
通常 IDEs 使用 maven pom.xml 文件作为项目配置的来源。
在 IDE 中更改编译器设置并不总是对 Maven 构建有影响。
这就是为什么要使项目始终可由 maven 管理(并可与其他 IDE 互操作)的最佳方法是编辑 pom.xml 文件并指示 IDE 与 maven 同步。
我在 IntelliJ、JDK1.8、maven 3.2.5 中使用 maven。出现编译错误:使用 -source 7 或更高版本启用 diamond opera。详情如下:
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] TrainingConstructor.java:[31,55] diamond operator is not supported in -source 1.5 (use -source 7 or higher to enable diamond operator)
[ERROR] DTM.java:[79,21] try-with-resources is not supported in -source 1.5 (use -source 7 or higher to enable try-with-resources)
[ERROR] ticons.java:[53,44] diamond operator is not supported in -source 1.5 (use -source 7 or higher to enable diamond operator)
有什么建议吗?是否有任何其他配置来设置此 -source 级别?似乎它不使用 java 1.8。
检查您的 maven-compiler-plugin
是如何配置的,它应该使用 java 版本 7 或更高版本:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
要获得更完整的答案,请参阅
您必须更改配置:
<project>
[...]
<build>
[...]
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
[...]
</build>
[...]
</project>
你应该了解JavaC中的source/taget
选项和JDK1.8/1.7等的用法的区别
除此之外,您应该升级使用 maven-compiler-plugin。
如果您已经尝试过@Sergey Pauk 和@khmarbaise 解决方案,请查看设置 -> 构建、执行、部署 -> 编译器 -> Java 编译器,有目标字节码版本特定模块
解决方案 1 - 在 pom.xml
中设置这些属性<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
解决方案 2 - 配置 Maven 编译器插件(始终在 pom.xml 中)
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
...
为什么会这样
出现问题是因为
[...] at present the default source setting is 1.5 and the default target setting is 1.5, independently of the JDK you run Maven with. If you want to change these defaults, you should set source and target as described in Setting the -source and -target of the Java Compiler.
以及最近的 Maven 版本:
Also note that at present the default source setting is 1.6 and the default target setting is 1.6, independently of the JDK you run Maven with. You are highly encouraged to change these defaults by setting source and target as described in Setting the -source and -target of the Java Compiler.
这就是为什么更改 JDK 对源级别没有影响。因此,您有几种方法可以告诉 Maven 使用哪个源代码级别。
JDK 要使用的版本?
如果像本例中那样设置目标 1.7,请确保 mvn 命令实际上是使用 jdk7(或更高版本)启动的
语言水平 IDE
通常 IDEs 使用 maven pom.xml 文件作为项目配置的来源。 在 IDE 中更改编译器设置并不总是对 Maven 构建有影响。 这就是为什么要使项目始终可由 maven 管理(并可与其他 IDE 互操作)的最佳方法是编辑 pom.xml 文件并指示 IDE 与 maven 同步。