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

简介: 在用maven编译项目是由于项目中用了jdk 1.8, 编译是报错  -source 1.5 中不支持 lambda 表达式,Google找到这篇解决方案,记录一下:   编译时报如下错误: [ERROR] COMPILATION ERROR : [INFO] --------------...

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

 

相关文章
|
Java Maven 开发工具
【开发工具】解决idea maven报错: 无效的目标发行版:19
【开发工具】解决idea maven报错: 无效的目标发行版:19
935 0
|
Java Maven
maven报错:[ERROR] 不再支持源选项 7。请使用 8 或更高版本。
maven报错:[ERROR] 不再支持源选项 7。请使用 8 或更高版本。
434 0
|
1月前
|
Java Maven
Maven编译报错
Maven编译报错
55 1
|
4月前
|
缓存 IDE Java
maven install报错原因揭秘:‘parent.relativePath‘指向错误的本地POM文件
在使用Maven构建项目时,遇到&#39;parent.relativePath&#39;错误通常是由于父项目POM路径设置错误、版本不一致或内容不匹配导致的。解决方法包括:校正父项目POM的相对路径、确保版本一致、保持POM文件内容同步,并排查其他潜在问题,如子模块命名冲突和Maven缓存问题。通过这些步骤可解决该错误,避免项目构建失败。
maven install报错原因揭秘:‘parent.relativePath‘指向错误的本地POM文件
|
5月前
|
Java Maven
Maven配置以及IDEA设置(Cannot resolve plugin org.apache.maven.plugins:报错)
Maven配置以及IDEA设置(Cannot resolve plugin org.apache.maven.plugins:报错)
524 1
|
3月前
|
Java Maven 编译器
Java编译器注解运行和自动生成代码问题之Maven编译时设置生成的源码的文件夹路径问题如何解决
Java编译器注解运行和自动生成代码问题之Maven编译时设置生成的源码的文件夹路径问题如何解决
|
4月前
|
运维 安全 Java
阿里云云效操作报错合集之maven的setting.xml 上,本地拉取时,报401问题,该怎么办
本合集将整理呈现用户在使用过程中遇到的报错及其对应的解决办法,包括但不限于账户权限设置错误、项目配置不正确、代码提交冲突、构建任务执行失败、测试环境异常、需求流转阻塞等问题。阿里云云效是一站式企业级研发协同和DevOps平台,为企业提供从需求规划、开发、测试、发布到运维、运营的全流程端到端服务和工具支撑,致力于提升企业的研发效能和创新能力。
|
3月前
|
运维 Java Devops
阿里云云效操作报错合集之进行Maven打包时,遇到报错"ERR: No email loop found by ",该怎么办
本合集将整理呈现用户在使用过程中遇到的报错及其对应的解决办法,包括但不限于账户权限设置错误、项目配置不正确、代码提交冲突、构建任务执行失败、测试环境异常、需求流转阻塞等问题。阿里云云效是一站式企业级研发协同和DevOps平台,为企业提供从需求规划、开发、测试、发布到运维、运营的全流程端到端服务和工具支撑,致力于提升企业的研发效能和创新能力。
|
4月前
|
缓存 运维 负载均衡
阿里云云效操作报错合集之在获取Maven私有库配置出错,该如何操作
本合集将整理呈现用户在使用过程中遇到的报错及其对应的解决办法,包括但不限于账户权限设置错误、项目配置不正确、代码提交冲突、构建任务执行失败、测试环境异常、需求流转阻塞等问题。阿里云云效是一站式企业级研发协同和DevOps平台,为企业提供从需求规划、开发、测试、发布到运维、运营的全流程端到端服务和工具支撑,致力于提升企业的研发效能和创新能力。
|
5月前
|
缓存 弹性计算 Java
云效产品使用报错问题之maven仓设置为私有后,赋予成员权限未生效,拉包401要如何解决
本合集将整理呈现用户在使用过程中遇到的报错及其对应的解决办法,包括但不限于账户权限设置错误、项目配置不正确、代码提交冲突、构建任务执行失败、测试环境异常、需求流转阻塞等问题。阿里云云效是一站式企业级研发协同和DevOps平台,为企业提供从需求规划、开发、测试、发布到运维、运营的全流程端到端服务和工具支撑,致力于提升企业的研发效能和创新能力。