简介

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. pycharm中自定义函数补全

    在 PyCharm 中,你可以通过以下步骤实现这一目标: 打开 PyCharm,点击顶部菜单的 "File"(文件) -> "Settings"(设置). ...

  2. Django多方式登录认证

    多方式认证原理 我们平常使用的Django登录认证是django.contrib.auth.authenticate 点进去源码,我们会看到这个函数,真正的认证类是它里面的那个 继续点进去源码,会看到 ...

  3. 使用原生 cookieStore 方法,让 Cookie 操作更简单

    前言 对于前端来讲,我们在操作cookie时往往都是基于document.cookie,但它有一个缺点就是操作复杂,它并没有像localStorage那样提供一些get或set等方法供我们使用.对与c ...

  4. 老生常谈的iOS- weak原理,你真的懂得还是为了应付面试

    前言 weak对于iOS开发来说只要解决一些对象相互引用的时候,避免出现强强引用,对象不能被释放,出现内存泄露的问题. weak 关键字的作用域弱引用,所引用对象的计数器不会加一,并在引用对象被释放的 ...

  5. 【Azure Redis】PHPRedis遇见SSL Connection Timeout问题

    问题描述 PHP Redis客户端遇见使用SSL Connection timeout,遇见问题后,切换回去Non-SSL没有出现问题.但是切换回SSL后,还是偶尔遇见Connection timeo ...

  6. 【Azure App Service for Linux】Linux Web App如何安装系统未安装的包

    问题描述 Linux Web App中如何安装系统默认未安装的包,如何来执行如 apt install XXX命令呢?现在遇见的问题时,通过Azure App Service门户中的SSH登录后,执行 ...

  7. 【Azure Redis 缓存】Redis导出数据文件变小 / 在新的Redis复原后数据大小压缩近一倍问题分析

    问题描述 使用 Azure Cache for Redis 服务,在两个Redis服务之间进行数据导入和导出测试.在Redis中原本有7G的数据值,但是导出时候发现文件大小仅仅只有30MB左右,这个压 ...

  8. 有了 ETL 数据神器 dbt,表数据秒变 NebulaGraph 中的图数据

    本文搭配同主题分享视频阅读更佳,<多数据源的数据治理实践> 如果你装好某款数据库产品,比如:分布式图数据库 NebulaGrpah,跃跃欲试的第一步是不是就让它干活搞数据呢?好的,现在问题 ...

  9. C++ STL 容器-Vector类型

    C++ STL 容器-Vector类型 std::vector是C++标准库中的一个动态数组容器,它提供了随机访问迭代器,因此你可以像使用普通数组一样使用vector. vector容器可以动态地增长 ...

  10. 二: sql模式(sql_mode)

    # sql_mode 1 介绍 sql_mode 会影响 MySQL支持的SQL语法以及它执行的数据验证检查.通过设置sql_mode,可以完成不同严格程度 的数据校验,有效地保障数据准确性. MyS ...