【Exception】maven-compiler-plugin 编译失败集锦

版权声明:本文为【丶我们一起学猫叫】原创文章,允许转载,但转载必须注明出处并附带首发链接! https://blog.csdn.net/AV_woaijava/article/details/89323556

1

JDK 明明是1.8为什么说编译环境和运行环境不一致?What fuck ?
在这里插入图片描述
JDK 明明1.8为什么编译环境变成1.5了?What fuck ?
在这里插入图片描述

原因分析:

奇怪的是我的机器上只安装了 JDK 8,为什么还会说不支持 diamond 和 lambda 呢?
在 Google 大神的指引下,在 Maven Compiler 插件介绍 里面找到了答案:
Also note that at present the default source setting is 1.5 and thedefault target setting is 1.5, independently of the JDK you run Maven with.

原来 Maven Compiler 插件默认会加 -source 1.5 及 -target 1.5 参数来编译(估计是为了兼容一些比较老的 Linux 服务器操作系统,它们通常只有 JDK 5),而我们的代码里使用了 JDK8 的语法

解决方案:

pom.xml中添加如下

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <!--指定source版本-->
                <source>1.8</source>
                <!--指定target版本-->
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

猜你喜欢

转载自blog.csdn.net/AV_woaijava/article/details/89323556