yaml语法Maven中设置Java版本

使用Compiler Plugin

Maven Compiler 命令接受 -source 和 - target 版本参数,默认情况,两个参数都为1.6

1
2
3
4
<properties>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.source>1.8</maven.compiler.source>
</properties>
1
2
3
4
5
6
7
8
9
<plugins>
    <plugin>    
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
        </configuration>
    </plugin>
</plugins>

Java9及以上版本

从java 9 开始,可以使用 -release命令行参数

1
2
3
<properties>
    <maven.compiler.release>7</maven.compiler.release>
</properties>

maven-compiler-plugin版本3.6以上

1
2
3
4
5
6
7
8
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.0</version>
    <configuration>
        <release>7</release>
    </configuration>
</plugin>

Spring Boot 项目

Spirng Boot 应用在pom.xml的properties中定义了JDK版本,我们的项目需要添加*spring-boot-starter-parent*作为父模块。

1
2
3
4
5
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.5.RELEASE</version>
</parent>

spring boot 默认java版本为1.8,可通Java.version属性修改.

1
2
3
<properties>
    <java.version>1.9</java.version>
</properties>