Apache Maven指定JDK版本

設定專案JDK版本

前言

使用Apcehe Maven建置專案有時候建置出來使用的JDK版本並不是自己所想的,此時可以透過pom.xml進行相關設定指定自己所使用的JDK版本。

實作

要設定自己所使用的 JDK 版本可以透過以下兩種設定方式達成。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
<build>
    <plugins>
        <!-- 使用 JDK 11 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
            <configuration>
                <source>11</source>
                <target>11</target>
            </configuration>
        </plugin>
    </plugins>
</build>

或是寫得更簡單點。

1
2
3
4
<properties>
    <maven.compiler.target>11</maven.compiler.target>
    <maven.compiler.source>11</maven.compiler.source>
</properties>