到底如何配置 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>
原文地址:https://www.cnblogs.com/lhat/p/12157124.html