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

版权声明:个人随笔,在工作中遇到的问题,只为保存文档,希望能对你有帮助,如有错误烦请指正一同进步!谢谢! https://blog.csdn.net/w893932747/article/details/90518725

题记:

       新接了一个项目,居然每次更新Idea的编译版本都会自动改动,经过艰苦奋斗终于搞定!特此分享

报错:

      maven更新以后Java的编译版本会自动修改。如下图:我本地只有jdk1.8但是它居然会报1.6的版本异常,这就有点MMP了。

原因:

   官方说法:

Also note that at present the default source setting is 1.6 and the default target setting is 1.6, independently of the JDK you run Maven with. You are highly encouraged to change these defaults by setting source and target as described in Setting the -source and -target of the Java Compiler.

翻译:     

目前默认的源设置为1.6,默认目标设置为1.6,与您运行Maven的JDK无关。 强烈建议您通过设置源代码和目标来更改这些默认值,如设置Java编译器的-source和-target中所述。

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

解决方法:

修改maven全局的jdk版本或者配置局部的jdk版本

一、配置全局的jdk版本(在maven的setting.xml文件中添加)

<profile>  
    <id>jdk17</id>  
    <activation>  
        <activeByDefault>true</activeByDefault>  
        <jdk>1.7</jdk>  
    </activation>  
    <properties>  
        <maven.compiler.source>1.8</maven.compiler.source>  
        <maven.compiler.target>1.8</maven.compiler.target>  
        <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>  
    </properties>   
</profile>

二、配置局部的jdk版本(在项目中配置jdk版本信息)

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

OK,问题解决!

猜你喜欢

转载自blog.csdn.net/w893932747/article/details/90518725
今日推荐