配置 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