简介

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的使用,希望大家能够在实际工作中用起来。

更多精彩内容且看:

本文作者:flydean程序那些事

本文链接:http://www.flydean.com/apache-maven-toolchains/

本文来源:flydean的博客

欢迎关注我的公众号:程序那些事,更多精彩等着您!

Apache Maven ToolChains的使用的更多相关文章

  1. 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- ...

  2. 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 ...

  3. 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:/ ...

  4. 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 ...

  5. [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 ...

  6. 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 ...

  7. [Maven]Apache Maven 入门篇

    作者:George Ma 上 写这个 maven 的入门篇是因为之前在一个开发者会的动手实验中发现挺多人对于 maven 不是那么了解,所以就有了这个想法.这个入门篇分上下两篇.本文着重动手,用 ma ...

  8. apache maven pom setting

    <?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Soft ...

  9. [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 ...

  10. Apache Maven 入门篇 ( 上 )

    作者:George Ma 写这个 maven 的入门篇是因为之前在一个开发者会的动手实验中发现挺多人对于 maven 不是那么了解,所以就有了这个想法. 这个入门篇分上下两篇.本文着重动手,用 mav ...

随机推荐

  1. React 组件通信方式

    人生的游戏不在于拿了一副好牌,而在于怎样去打好坏牌,世上没有常胜将军,勇于超越自我者才能得到最后的奖杯. 1. 父子组件通信方式 1.1 父组件传递到子组件 直接通过属性进行传递,数据的传递可以提高组 ...

  2. 【C# .Net】List循环add,出现数据相同现象? 引发对引用类型和值类型的底层逻辑的思考。

    赶项目时发现了一个问题,定义一个引用对象,如果在循环外定义对象,在循环内list.add(object).最后的结果却是所有的对象值都是一样的,即每add一次,都会把之前的数据覆盖. 解决方法:把对象 ...

  3. Long和int比较用==还是用equals

    应该用==,因为equels会先比较类型,这样值一样的不同类型的数字就直接返回false啦.看源码吧. public boolean equals(Object obj) { System.out.p ...

  4. 浅入kubernetes(3):namespace、node、pod

    目前已经完成三篇关于 kubernetes 的文章: 在 Ubuntu 上安装 K8S教程 浅入kubernetes(1):Kubernetes 入门基础 浅入kubernetes(2):Kubern ...

  5. C++ STL 容器-string类型

    C++ STL 第一部分-容器 STL的介绍 C++的STL分为六大部分 容器分为 容器的概念 容器内元素的条件 1.必须可以复制copy或者搬移move,包括条件是在拷贝和搬移的过程中不存在副作用. ...

  6. hadoop 3.3.5伪分布式集群部署以及遇到的问题解决

    hadoop包下载 https://archive.apache.org/dist/hadoop/common/ 安装好jdk并配置环境变量 下载hadoop压缩包并放至 /data/hadoop目录 ...

  7. 使用Kubernetes搭建带有ik分词的Elasticsearch集群

    创建好带有Ik分词的es镜像,并上传到镜像仓库中,创建镜像可参考链接中的文档 https://www.cnblogs.com/hi-lijq/p/16895206.html 编写es_cluster- ...

  8. TCP 中的 Delay ACK 和 Nagle 算法

    哈喽大家好,我是咸鱼. 今天分享一篇大佬的文章,作者:卡瓦邦噶! 文章链接:https://www.kawabangga.com/posts/5845 教科书介绍的 TCP 内容通常比较基础:包括三次 ...

  9. 牛客周赛 Round 31(A~F)

    目录 A B C D E F A #include <bits/stdc++.h> #define int long long #define rep(i,a,b) for(int i = ...

  10. beanstalkd轻量级消息队列的安装

    1.版本介绍 CentOS:CentOS Linux release 7.9.2009 (Core) beanstalkd:beanstalkd 1.10 2.安装 (1)先安装epel-releas ...