Maven编译错误:(使用-source 7或更高版本启用菱形运算符)

时间:2015-03-25 14:13:34

标签: java maven intellij-idea maven-3

我在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)

有什么建议吗?是否有其他配置来设置此-source级别?好像它不使用java 1.8。

4 个答案:

答案 0 :(得分:61)

检查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>

如需更完整的答案,请参阅the one below

答案 1 :(得分:46)

为什么会发生

问题出现是因为

  

[...]目前默认的源设置为1.5,默认目标设置为1.5,独立于您运行Maven的JDK。如果要更改这些默认值,则应该设置源代码和目标,如设置Java编译器的-source和-target所述。

     

Maven Compiler Plugin Introduction

这就是为什么更改JDK对源级别没有影响的原因。所以你有几种方法可以告诉Maven使用什么源代码。

  • 在pom.xml中配置Maven编译器插件
<build>

<plugins>
    <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
            <source>1.7</source>
            <target>1.7</target>
        </configuration>
    </plugin>
</plugins>
...
  • 或设置这些属性(始终在pom中)
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>

JDK VERSION使用?

如果您设置目标1.7,就像在此示例中一样,请确保mvn命令实际上是使用jdk7(或更高版本)启动的

IDE上的语言级别

通常,IDE使用maven pom.xml文件作为项目配置的源。 在IDE中更改编译器设置并不总是对maven构建有效。 这就是为什么保持项目始终可以通过maven管理(并与其他IDE互操作)的最佳方法是编辑pom.xml文件并指示IDE与maven同步。

答案 2 :(得分:5)

您必须更改配置:

<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。

答案 3 :(得分:1)

如果您已经尝试过@Sergey Pauk和@khmarbaise解决方案,请查看设置 - &gt;构建,执行,部署 - &gt;编译器 - &gt; Java编译器,特定模块有目标字节码版本

相关问题