您的位置:首页 > 其它

maven编译报错 -source 1.5 中不支持 lambda 表达式

2017-08-18 14:29 746 查看
在用maven编译项目是由于项目中用了jdk 1.8, 编译是报错 -source 1.5 中不支持 lambda 表达式,Google找到这篇解决方案,记录一下:

编译时报如下错误:

[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] AAA\.jenkins\workspace\BBB\CCC.java:[73,46] 错误: -source 1.5 中不支持 diamond 运算符
[ERROR]   (请使用 -source 7 或更高版本以启用 diamond 运算符)
[ERROR] AAA\.jenkins\workspace\BBB\DDD.java:[38,33] 错误: -source 1.5 中不支持 lambda 表达式
[ERROR]   (请使用 -source 8 或更高版本以启用 lambda 表达式)


奇怪的是我的 Jenkins 构建机器上只安装了 JDK 8,为什么还会说不支持 diamond 和 lambda 呢?在 Google 大神的指引下,在 Maven Compiler 插件介绍 里面找到了答案:
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.


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

<project>
[...]
<build>
[...]
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
[...]
</build>
[...]
</project>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐