Error:source 1.5 中不支持 diamond 运算符

从码云上拉下来一个项目运行的时候,报了错误:

我的jdk是用的1.8,我知道1.8是允许这种写法的,但是为什么会报这个错误呢,我File -> Project Structure

看到依赖是用的1.8

但是,它明明告诉我,用的是1.5

点开Sources:

发现Language level 被指定为1.5 ,但是在我另外一台电脑上明明是可以的,是什么时候被重新指定为1.5 了呢。

研究发现,可能和Maven有关系。

我用的是 maven-compiler-plugin:3.1 (网络原因,报红,不过和这个没关系)

参考官网的介绍:http://maven.apache.org/plugins-archives/maven-compiler-plugin-3.1/

Maven Compiler Plugin

The Compiler Plugin is used to compile the sources of your project. Since 3.0, the default compiler is javax.tools.JavaCompiler (if you are using java 1.6) and is used to compile Java sources. If you want to force the plugin using javac, you must configure the plugin option forceJavacCompilerUse.

Also note that 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.

Other compilers than javac can be used and work has already started on AspectJ, .NET, and C#.

This Compiler Plugin corresponds to Maven 1.x's Java Plugin.

NOTE: To know more about the JDK javac, please see: http://download.oracle.com/javase/6/docs/technotes/tools/windows/javac.html.

大意是,从3.0 开始, 默认源码的设置级别是1.5,与运行Maven的jdk版本无关。

如果不特别指定,Language level 的级别会一直是1.5 ,可能会引起一些不必要的麻烦,所以我们一般会在项目的pom文件里指定源码的jdk版本。

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

报错消失:

总结:

这个可能是我们平常不太注意的点,但遇到的时候,可能会一头雾水,其实还是我们对Maven项目构建的一些知识点了解不够深入。

posted @ 2020-05-31 21:32  大水煮鱼  阅读(2468)  评论(0编辑  收藏  举报