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

http://blog.csdn.net/kai161/article/details/50379418

Setting the -source and -target of the Java Compiler

Sometimes when you may need to compile a certain project to a different version than what you are currently using. The javac can accept such command using -source and -target. The Compiler Plugin can also be configured to provide these options during compilation.

For example, if you want to use the Java 8 language features (-source 1.8) and also want the compiled classes to be compatible with JVM 1.8 (-target 1.8), you can either add the two following properties, which are the default property names for the plugin parameters:

<project>
[...]
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
[...]
</project>

or configure the plugin directly:

<project>
[...]
<build>
[...]
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
[...]
</build>
[...]
</project>

Note: Merely setting the target option does not guarantee that your code actually runs on a JRE with the specified version. The pitfall is unintended usage of APIs that only exist in later JREs which would make your code fail at runtime with a linkage error. To avoid this issue, you can either configure the compiler's boot classpath to match the target JRE or use the Animal Sniffer Maven Plugin to verify your code doesn't use unintended APIs. In the same way, setting the sourceoption does not guarantee that your code actually compiles on a JDK with the specified version. To compile your code with a specific JDK version, different than the one used to launch Maven, refer to the Compile Using A Different JDK example.

http://maven.apache.org/components/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html

最新文章

  1. 网络抓包wireshark(转)
  2. iOS中“返回”操作相关
  3. Win10 UWP开发中的重复性静态UI绘制小技巧 1
  4. SU Demos-05Sorting Traces-02Demos
  5. 002 C#学前入门
  6. HBase with MapReduce (Read and Write)
  7. paip.myeclipse7 java webservice 最佳实践o228
  8. IOS显示九宫格列表
  9. 140304笔记, mysql 更改自动增长字段值的重新设定
  10. 对CURL的一些研究
  11. mongodb在java驱动包下的操作(转)
  12. Android群英传》读书笔记 (1) 第一章 Android体系与系统架构 + 第二章 Android开发工具新接触
  13. Swift类和结构体定义-备
  14. jquery之选项卡效果
  15. jQuery是什么
  16. 团队作业2--需求分析&amp;原型设计
  17. Flask基础
  18. High Availability手册(3): 配置
  19. [Codeforces394B]Very Beautiful Number(逆推)
  20. 关于用wkwebview加载沙盒documents下html文件 模拟器可以,真机不行的解决方案

热门文章

  1. 在IntelliJ IDEA中添加框架支持时找不到Hibernate的解决办法
  2. ibatis.net MVC 单元测试 错误解决方法
  3. 【CentOS】虚拟机网络配置与远程登录
  4. 图形化查看maven的dependency依赖
  5. 【Oracle】ora-28001 ora-28002提示用户密码即将过期的解决办法
  6. linux 添加 service 服务并自动添加 chkconfig 启动级别
  7. Mongoose 的实例方法中访问静态方法
  8. 关于history的Linux命令行
  9. *HDU1142 最短路+记忆化dfs
  10. 错误Batch update returned unexpected row count from update [0]; actual row count: 0;