升级maven编译插件_到底如何配置 maven 编译插件的 JDK 版本

本文介绍了如何配置Maven编译插件(maven-compiler-plugin)以使用特定的JDK版本,特别是针对3.8.0及更高版本的变化。内容包括在pom.xml中直接设置JDK版本,以及在SpringBoot项目中如何通过设置`maven.compiler.parameters`来覆盖默认配置,确保使用正确的JDK版本进行编译。
摘要由CSDN通过智能技术生成

配置 maven 编译插件的 JDK 版本

maven 编译插件(maven-compiler-plugin)有默认编译 JDK 的版本,但这个 JDK 版本通常和我们实际开发使用的不一致。

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

......

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版本

org.apache.maven.plugins

maven-compiler-plugin

3.8.0

1.8

1.8

SpringBoot 是如何配置 maven 编译插件的 JDK 版本的 ?

在 SpringBoot 项目中我们只需要如下配置,即可设定 maven 编译插件的 JDK 版本了

1.8

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

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

...

1.8==

...

${java.version}

...

${java.version}

......

maven-compiler-plugin

true

看来,关键点是这个 ,看文档是怎么说的

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

英文不好,准确意思翻译不出来。但是结合上面这些证据,连猜带蒙的大概能知道配置 有什么作用了。

maven 编译插件如果配置了 true,那么插件的配置就可以从用户属性中获取了。具体每个配置使用什么样的属性名称,在文档参数的 User Property 都有明确表示。

比如原先我们要 1.8 这样配置,现在使用 3.6.2 版本以上的 maven 编译插件,就可以在用户属性中 1.8。

SpringBoot 就是这么配置的! (在 SpringBoot 项目中设置 覆盖掉 spring-boot-starter-parent-2.x.x.RELEASE pom 中的属性)

怎么配置 maven 编译插件的 JDK 版本的

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

1.8

如果没有使用 SpringBoot,只是单纯的 maven 项目,那么如下配置(其实就是复制了 SpringBoot 的做法)

1.8

${java.version}

${java.version}

maven-compiler-plugin

true

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值