[英]Error:(24, 46) error: diamond operator is not supported in -source 1.6 (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或更高版本啟用菱形歌劇。 詳細信息如下:
[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)
有什么建議么? 還有其他配置可以設置此源級別嗎? 似乎它不使用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>
有關更完整的答案,請參見下面的答案。
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
...
出現問題是因為
目前,默認源設置為1.5,默認目標設置為1.5, 與運行Maven的JDK無關。 如果要更改這些默認值,則應按照設置Java編譯器的-source和-target中所述設置源和目標。
以及最新的Maven版本:
還要注意,當前的默認源設置為1.6,默認目標設置為1.6,與運行Maven的JDK無關。 強烈建議您按照設置Java編譯器的-source和-target中所述, 通過設置source和target來更改這些默認值 。
這就是為什么更改JDK對源代碼級別沒有影響的原因。 因此,您有兩種方法可以告訴Maven使用哪個源級別。
如果您將目標設置為1.7(如本例所示),請確保mvn命令實際上是使用jdk7(或更高版本)啟動的
通常,IDE使用maven pom.xml文件作為項目配置的源。 在IDE中更改編譯器設置並不總是會影響maven構建。 因此,使項目始終可通過Maven進行管理(以及與其他IDE互操作)的最佳方法是編輯pom.xml文件,並指示IDE與maven同步。
您必須更改配置:
<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
選項與JDK 1.8 / 1.7等的用法之間的區別。
除此之外,您還應該升級使用maven-compiler-plugin。
如果您已經嘗試過@Sergey Pauk和@khmarbaise解決方案,還可以在設置->構建,執行,部署->編譯器-> Java編譯器中查看,特定模塊有目標字節碼版本
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.