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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29258141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 07:41:01  来源:igfitidea点击:

Maven Compilation Error: (use -source 7 or higher to enable diamond operator)

javamavenintellij-ideamaven-3

提问by HappyCoding

I'm using maven in IntelliJ, JDK1.8, maven 3.2.5. Got compilation error: use -source 7 or higher to enable diamond opera. details are as follows:

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

Any suggestions? Is there any other configuration to set this -source level? seems it doesn't use java 1.8.

有什么建议?是否还有其他配置可以设置此 -source 级别?似乎它不使用 java 1.8。

采纳答案by Sergey Pauk

Check how your maven-compiler-pluginis configured, it should use java version 7 or higher:

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

For a more complete answer see the one below.

有关更完整的答案,请参阅下面的答案。

回答by khmarbaise

You have to change your configuration:

您必须更改配置:

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

You should learn the difference between source/tagetoption in JavaC and the usage of JDK 1.8/1.7 etc.

您应该了解source/tagetJavaC 中的选项和 JDK 1.8/1.7 等的用法之间的区别。

Apart from that you should upgrade the use maven-compiler-plugin.

除此之外,您应该升级使用 maven-compiler-plugin。

回答by pezetem

If You already tried the @Sergey Pauk and @khmarbaise solution, take a look also in the settings -> Build, Execution, Deployment -> Compiler -> Java Compiler, there are target bytecode versions for particular modules

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

回答by Fabio Bonfante

SOLUTION 1 - Set these properties in the pom.xml

解决方案 1 - 在 pom.xml 中设置这些属性

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

SOLUTION 2 - Configure the Maven compiler plugin (always in the pom.xml)

解决方案 2 - 配置 Maven 编译器插件(始终在 pom.xml 中)

<build>

<plugins>
    <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
            <source>1.7</source>
            <target>1.7</target>
        </configuration>
    </plugin>
</plugins>
...


WHY IT HAPPENS

为什么会发生

The problem arises because

问题出现是因为

[...] 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 Compiler Plugin Introduction (until version 3.3)

[...] 目前默认源设置为 1.5,默认目标设置为 1.5,与运行 Maven 的 JDK 无关。如果要更改这些默认值,则应按照设置 Java 编译器的 -source 和 -target 中的说明设置 source 和 target。

Maven Compiler Plugin 介绍(至3.3版)

and with recent Maven versions:

以及最近的 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 targetas described in Setting the -source and -target of the Java Compiler.

Maven Compiler Plugin Introduction

另请注意,目前默认源设置为 1.6,默认目标设置为 1.6,与运行 Maven 的 JDK 无关。强烈建议您通过设置源和目标来更改这些默认值,如设置 Java 编译器的 -source 和 -target 中所述。

Maven编译器插件介绍

That's why changing the JDK has no effect on the source level. So you have a couple of way to tell Maven what source level to use.

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

JDK VERSION TO USE?

要使用的 JDK 版本?

If you set a target 1.7 like in this example be sure that the mvn command is actually launched with a jdk7 (or higher)

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

LANGUAGE LEVEL ON THE IDE

IDE 的语言水平

Usually IDEs use maven pom.xml file as a source of project configuration. Changing the compiler settings in the IDE not always has effect on the maven build. That's why, the best way to keep a project always manageable with maven (and interoperable with other IDEs) is edit the pom.xml files and instruct the IDE to sync with maven.

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