到底如何配置 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
trueto 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>
到底如何配置 maven 编译插件的 JDK 版本的更多相关文章
- maven 编译插件指定jdk版本的两种方式
第一种方式: <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration&g ...
- 配置ant编译时的jdk版本
如下图,选择对应的sdk版本:
- maven 修改默认的JDK版本
maven jdk 版本配置 maven 默认使用的 jdk 版本 新建一个 maven 项目,如下 : 项目左下方出现一个感叹号,JRE 显示的是 1.5 版本.解决方式有两种,一种是配置 pom. ...
- Unsupported major.minor version 51.0 在配置/运行Maven工程时,JDK与Maven所引用的jdk版本不一致
在配置Maven工程,部署到tomcat服务器运行的过程中,遇到如下错误: "Unsupported major.minor version 51.0 " 错误原因是由于maven ...
- maven 配置环境变量后报错jdk版本不符
不需要再cmd 输入java -version,因为环境变量里配置的不一定是cmd显示版本,直接看环境变量改成maven要求的jdk版本即可.
- IntelliJ IDEA中Maven项目的默认JDK版本
在IntelliJ IDEA 15中使用Maven时,IDEA将默认的编译版本.源码版本设置为jdk5.编译项目的时候出现警告:"Warning:Java: 源值1.5已过时, 将在未来所有 ...
- maven常见问题处理(3-1)修改maven 默认使用的 jdk 版本
Eclipse工程应设定了1.8,maven编译仍然使用1.6的解决办法 解决方式有两种,一种是配置 pom.xml,一种是配置 settings.xml. 方式一:settings.xml 配置 打 ...
- 设置通过Maven创建工程的JDK版本
在eclipse中创建的Maven工程默认的运行环境为JDK1.5,如果想修改JDK的版本,无非是右键项目点击Build Path->Configure Build Path修改JDK信息-&g ...
- maven编译插件版本配置案例
<!-- Build Settings 构建设置 --> <build> <finalName>${project.artifactId}</finalNam ...
随机推荐
- Levenshtein distance 编辑距离算法
这几天再看 virtrual-dom,关于两个列表的对比,讲到了 Levenshtein distance 距离,周末抽空做一下总结. Levenshtein Distance 介绍 在信息理论和计算 ...
- React与Vue的相同与不同点
我们知道JavaScript是世界上最流行的语言之一,React和Vue是JS最流行的两个框架.所以要想前端的开发那么必须掌握好这两个框架. 那么这两个框架有什么不同呢? React 和 Vue 相同 ...
- 2018-8-10-win10-uwp-获得元素绝对坐标
title author date CreateTime categories win10 uwp 获得元素绝对坐标 lindexi 2018-08-10 19:16:51 +0800 2018-2- ...
- 2019-6-23-win10-uwp-解决-SerialDevice.FromIdAsync-返回空
title author date CreateTime categories win10 uwp 解决 SerialDevice.FromIdAsync 返回空 lindexi 2019-6-23 ...
- 2018-2-13-WPF-DelegateCommand-出现Specified-cast-is-not-valid
title author date CreateTime categories WPF DelegateCommand 出现Specified cast is not valid lindexi 20 ...
- H3C HDLC概述
- Python--day60--建立第一个Djiango项目
- P1045 和为给定数
题目描述 给出若干个整数,询问其中是否有一对数的和等于给定的数. 输入格式 共三行: 第一行是整数 \(n(0 \lt n \le 100000)\) ,表示有 \(n\) 个整数. 第二行是n个整数 ...
- tf.reduce_sum()
#axis 表示在哪个维度进行sum操作,不写代表所有维 #keep_dims 是否保留原始数据维度 reduce_sum( input_tensor, axis=None, keep_dims=Fa ...
- 2018.12.7 浪在ACM 集训队第八次测试赛
2018.12.7 浪在ACM 集训队第八次测试赛 https://blog.csdn.net/QLU_minoz/article/details/84886717 感谢苗学林同学C题和D题题解 ...