springboot-How to solve compilation error of springboot2 apps

1. Introduction

Sometimes, when we make or build our SpringBoot app, we got this Exception:

[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /Users/JavaProjects/springboot2-mvc/src/main/java/springboot2/mvc/PropertyLogger.java:[31,28] -source 1.5 do not support lambda expressions
  (please use -source 8 or bigger version to use lambda expression)
[ERROR] /Users/JavaProjects/springboot2-mvc/src/main/java/springboot2/mvc/PropertyLogger.java:[33,34] -source 1.5 do not support method reference
  (please use -source 8 or later to enable method reference)
[INFO] 2 errors 

2. Environments

  • SpringBoot 2.x

3. The solution

3.1 Check your Intelli Idea configurations

Check your Intellij Idea’s project source’s language level configurations like this: project

Check your Intellij Idea’s project module SDK configurations like this: project

3.2 solution 1: Add build plugin to your pom

By default, Maven 3 uses JDK 1.5 to compile the project, which is very old. Fortunately, Maven comes with a maven-compiler-plugin, which tells Maven to compile the project source with a specified JDK version.

Add this build plugin to your project:

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

3.3 solution 2: Add properties to your pom

Alternatively, you can add these properties to your pom :

<properties>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.source>1.8</maven.compiler.source>
</properties>

3.3 Rebuild your project

mvn clean comiple

We get this:

[INFO] BUILD SUCCESS

4. Summary

You can see that maven use the jdk 1.5 to build springboot apps by default, you can change this by use the maven-compiler-plugin or add maven.compiler properties to your pom.