Apache Maven ToolChains的使用
简介
Maven是java中非常有用和常用的构建工具,基本上现在大型的java项目都是Maven和gradle的天下了。
因为JDK的版本现在以每半年一次的速度在飞速发展。不同的JDK版本就有不同的java路径,我们在使用Maven的过程中,可能经常会需要切换JDK的版本。
一般来说我们可以在maven-compiler-plugin中配置好executable的路径。如下所示:
更多内容请访问www.flydean.com
<build>
<plugins>
<!-- target Java 14 -->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<!-- fork compilation and use the
specified executable -->
<fork>true</fork>
<executable>/usr/bin/javac14</executable>
</configuration>
</plugin>
</plugins>
</build>
看起来还不错,但是如果想切换executable的路径可能就比较麻烦。更有问题的是,如果你是团队来发,一个人在mac环境一个人在windows环境,两边的executable的路径完全是不同的,这会导致代码冲突,和代码难以维护。
Toolchains的介绍
为了解决这个问题,Maven为我们推出了Toolchains。使用Toolchains,我们可以将这些可执行文件的路径,版本号,还有类型都定义在一个toolchains.xml文件里面。
而在pom.xml文件中只需要引用toolchains.xml中定义的别名就可以了。
针对上面的windows和linux路径不一致的问题,我们可以保证pom.xml是完全一致的,大家只需要适配自己的toolchains.xml文件即可。
Toolchains的例子
Toolchains是和pom中其他的plugin结合起来使用的,比如最常用的maven-compiler-plugin。
下面我们举一个例子来说明。首先定义toolchains.xml文件,这个文件最好放在${user.home}/.m2/中。
<?xml version="1.0" encoding="UTF8"?>
<toolchains>
<!-- JDK toolchains -->
<toolchain>
<type>jdk</type>
<provides>
<version>14</version>
<vendor>oracle</vendor>
</provides>
<configuration>
<jdkHome>/path/to/jdk/14</jdkHome>
</configuration>
</toolchain>
<toolchain>
<type>jdk</type>
<provides>
<version>11</version>
<vendor>oracle</vendor>
</provides>
<configuration>
<jdkHome>/path/to/jdk/11</jdkHome>
</configuration>
</toolchain>
</toolchains>
上面的例子中,我们定义了2个JDK的toolchains。一个JDK14,一个JDK11。下面看下怎么在pom文件中使用。
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-toolchains-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<goals>
<goal>toolchain</goal>
</goals>
</execution>
</executions>
<configuration>
<toolchains>
<jdk>
<version>14</version>
<vendor>oracle</vendor>
</jdk>
</toolchains>
</configuration>
</plugin>
...
</plugins>
上面的pom配置文件中,我们通过简单的引用toolchains中的定义,即可无缝的进行JDK版本的切换。
Toolchains支持
Toolchains需要Maven 2.0.9以上版本的支持。
Toolchains是需要和pom中的plugin一起使用的,下面的图中列出了toolchains支持的plugin名字和最低的版本要求。

总结
本文介绍了Apache Maven中toolchain的使用,希望大家能够在实际工作中用起来。
更多精彩内容且看:
- 区块链从入门到放弃系列教程-涵盖密码学,超级账本,以太坊,Libra,比特币等持续更新
- Spring Boot 2.X系列教程:七天从无到有掌握Spring Boot-持续更新
- Spring 5.X系列教程:满足你对Spring5的一切想象-持续更新
- java程序员从小工到专家成神之路(2020版)-持续更新中,附详细文章教程
本文作者:flydean程序那些事
本文链接:http://www.flydean.com/apache-maven-toolchains/
本文来源:flydean的博客
欢迎关注我的公众号:程序那些事,更多精彩等着您!
Apache Maven ToolChains的使用的更多相关文章
- maven install Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.1.1:war (default-war) on project web_nanchang
maven打包成war时,报错:Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.1.1:war (default- ...
- Failed to execute goal org.apache.maven.plugins:maven-antrun-plugin:1.7:run (dist) on project hadoop-kms: An Ant BuildException has occured
编译cdh版hadoop2.5.0出现的问题 系统: CentOs66 64位 JDK:1.7 Maven: 3.0.5 Protobuf: libprotoc 2.5.0 编译命令: mvn pac ...
- eclipse加载maven工程提示pom.xml无法解析org.apache.maven.plugins:maven-resources-plugin:2.4.3解决方案
pom文件提示信息: Failure to transfer org.apache.maven.plugins:maven-resources-plugin:pom:2.4.3 from http:/ ...
- Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project LogTest: Compilation failure -> [Help 1]
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default ...
- [ERROR] Failed to execute goal org.apache.maven.plugins:maven-install-plugin:2.3.1:
security-sdk-1.0.jar已经存在于D:/secServerSDK-test/src/main/resources/lib下 报错如下: xxxxxx@xxxxxxxx /d/secSe ...
- maven Error resolving version for plugin 'org.apache.maven.plugins:maven-eclipse-plugin' from the repositories 解决
报错:Error resolving version for plugin 'org.apache.maven.plugins:maven-eclipse-plugin' from the repos ...
- [Maven]Apache Maven 入门篇
作者:George Ma 上 写这个 maven 的入门篇是因为之前在一个开发者会的动手实验中发现挺多人对于 maven 不是那么了解,所以就有了这个想法.这个入门篇分上下两篇.本文着重动手,用 ma ...
- apache maven pom setting
<?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Soft ...
- [ERROR] Failed to execute goal org.apache.maven.plugins:maven-archetype-plugin:2.4:create (default-cli) on project standalone-pom: Unable to parse configuration of 3: mojo org.apache.maven.plugins:
问题: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-archetype-plugin:2.4:create (defau ...
- Apache Maven 入门篇 ( 上 )
作者:George Ma 写这个 maven 的入门篇是因为之前在一个开发者会的动手实验中发现挺多人对于 maven 不是那么了解,所以就有了这个想法. 这个入门篇分上下两篇.本文着重动手,用 mav ...
随机推荐
- nginx中使用perl脚本来定制一些请求转发等等
http://t.zoukankan.com/carriezhangyan-p-9359708.html https://blog.csdn.net/weixin_28917223/article/d ...
- 正则表达式re模块---day18
1.匹配单个字符 import re lst = re.findall(正则表达式,要匹配的字符串) 返回的是列表,按照正则表达式匹配到的内容都扔到列表中 # ### 1.预定义字符集 # \d 匹配 ...
- 用宝塔设立分发Directory.Build.props及其Import文件的网站
新建站点 服务器名称我们约定是dev.amihome.cn 创建的默认站点有下面4个文件 把本地的文件,用宝塔上传 上图我们是把本地的Directory.Build.props文件上传到了网站的根目录 ...
- Vue源码学习(十):关于dep和watcher使用的一些思考
好家伙, 前面想了好久,都没想明白为什么要dep和watcher打配合才能实现数据-视图同步 为什么要多一个依赖管理这样的东西 给每个数据绑个watcher(xxfunction),然后,数据变了 ...
- 第123篇: JS函数属性与方法
好家伙,本篇为<JS高级程序设计>第十章"函数"学习笔记 ECMAScript 中的函数是对象,因此有属性和方法. 1.函数属性 每个函数都有两个属性:length 和 ...
- 【Azure 应用服务】在创建Web App Service的时候,选Linux系统后无法使用Mysql in App
问题描述 如图上,是App Services在Windows环境中,系统自带了MySQL In App功能.而在,Linux环境中,没有发现Mysql in App功能,是不是无法在Linux中使用呢 ...
- 【Azure Developer】PHP网站使用AAD授权登录的参考示例
问题描述 如果有个PHP网站,需要使用AAD授权登录,有没有PHP代码实例 可供参考呢? 参考代码 参考一篇博文(Single sign-on with Azure AD in PHP),学习使用SS ...
- Android Handler实现子线程与子线程、主线程之间通信
一.子线程向主线程传值: 首选在主线程里创建一个Handler 1 Handler mHandler = new Handler(){ 2 @Override 3 public void handle ...
- 跨域测试代码 - console 里面直接就可以测试
跨域测试代码 - console 里面直接就可以测试 var xhr = new XMLHttpRequest(); xhr.open("GET", "https://w ...
- Spring Boot学习日记13
学习引入Thymeleaf Thymeleaf 官网:https://www.thymeleaf.org/ Thymeleaf 在Github 的主页:https://github.com/thyme ...