到底如何配置 maven 編譯插件的 JDK 版本


千言萬語不及官方文檔,詳情請閱讀 compiler:compile 文檔

配置 maven 編譯插件的 JDK 版本

maven 編譯插件(maven-compiler-plugin)有默認編譯 JDK 的版本,但這個 JDK 版本通常和我們實際開發使用的不一致。

compiler:compile 文檔 有2個參數說明了編譯 JDK 版本

<source>

The -source argument for the Java compiler.

NOTE: Since 3.8.0 the default value has changed from 1.5 to 1.6

  • Type: java.lang.String
  • Since: 2.0
  • Required: No
  • User Property: maven.compiler.source
  • Default: 1.6

......

<target>

The -target argument for the Java compiler.

NOTE: Since 3.8.0 the default value has changed from 1.5 to 1.6

  • Type: java.lang.String
  • Since: 2.0
  • Required: No
  • User Property: maven.compiler.target
  • Default: 1.6

如上所述,從 maven-compiler-plugin3.8.0 之后,默認編譯 JDK 版本就由 1.5 改為 1.6 了。但是這仍然跟不上 JDK 的更新速度,目前大多數系統都在使用 JDK1.8

注意:User Property 這個說明,下面會用到

可以在 pom.xml 中這樣配置,修改 maven 編譯JDK版本

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

SpringBoot 是如何配置 maven 編譯插件的 JDK 版本的 ?

在 SpringBoot 項目中我們只需要如下配置,即可設定 maven 編譯插件的 JDK 版本了

    <properties>
        <java.version>1.8</java.version>
    </properties>

那么,SpringBoot 是怎么做到的呢?

查看 spring-boot-starter-parent-2.x.x.RELEASE 的 pom 文件

    <properties>
        ...
        <java.version>1.8</java.version>==
        ...
        <maven.compiler.source>${java.version}</maven.compiler.source>
        ...
        <maven.compiler.target>${java.version}</maven.compiler.target>
    </properties>
	......
	<build>
		 <pluginManagement>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <parameters>true</parameters>
                    </configuration>
                </plugin>
             </plugins>
	</build>

看來,關鍵點是這個 <parameters> ,看文檔是怎么說的

<parameters>

Set to true to generate metadata for reflection on method parameters.

  • Type: boolean
  • Since: 3.6.2
  • Required: No
  • User Property: maven.compiler.parameters
  • Default: false

英文不好,准確意思翻譯不出來。但是結合上面這些證據,連猜帶蒙的大概能知道配置 <parameters> 有什么作用了。

maven 編譯插件如果配置了 <parameters>true</parameters>,那么插件的配置就可以從用戶屬性中獲取了。具體每個配置使用什么樣的屬性名稱,在文檔參數的 User Property 都有明確表示。

比如原先我們要 <source>1.8</source> 這樣配置,現在使用 3.6.2 版本以上的 maven 編譯插件,就可以在用戶屬性中 <maven.compiler.source>1.8</maven.compiler.source>

SpringBoot 就是這么配置的! (在 SpringBoot 項目中設置 <java.version> 覆蓋掉 spring-boot-starter-parent-2.x.x.RELEASE pom 中的屬性)

怎么配置 maven 編譯插件的 JDK 版本的

如果使用了 SpringBoot ,那么只需在 pom.xml 如下配置

    <properties>
        <java.version>1.8</java.version>
    </properties>

如果沒有使用 SpringBoot,只是單純的 maven 項目,那么如下配置(其實就是復制了 SpringBoot 的做法)

    <properties>
        <java.version>1.8</java.version>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
    </properties>

	<build>
		 <pluginManagement>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <parameters>true</parameters>
                    </configuration>
                </plugin>
             </plugins>
        </pluginManagement>
	</build>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM