Java8系列(错误解决)--maven编译时"-source 1.5 中不支持 lambda 表达式"问题的解决

1 问题描述

调用Java8的Stream API,进行maven编译时,报错如下:

[INFO] -------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.2:compile (default-compile) on project Java8SerialLearning: Compilation failure
[ERROR] /home/gaoziqiang/eclipseWorkspace_project/Java8SerialLearning/src/main/java/cn/edu/sdut/softlab/Java8/Stream/ForEachDemo.java:[8,36] -source 1.5 中不支持 lambda 表达式

另外,我的Java配置如下:

java version "1.8.0_111"
Java(TM) SE Runtime Environment (build 1.8.0_111-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.111-b14, mixed mode)

2 问题原因

maven compiler插件默认的-source和-target参数都是1.5的,即:

      <plugin>
        <groupId>org.apache.maven.pluginsgroupId>
        <artifactId>maven-compiler-pluginartifactId>
        <version>3.2version>
        <configuration>
          <source>1.5source>
          <target>1.5target>
        configuration>
      plugin>

而这里需要的应该是1.8版本的。

3 问题解决

修改默认的maven插件的1.5版本,改为1.8即可了。如下:

      <plugin>
        <groupId>org.apache.maven.pluginsgroupId>
        <artifactId>maven-compiler-pluginartifactId>
        <version>3.2version>
        <configuration>
          <source>1.8source>
          <target>1.8target>
        configuration>
      plugin>

你可能感兴趣的:(maven,java8)