Migrating from Ant to Gradle is very easy with the importBuild method from AntBuilder. We only have to add this single line and reference our existing Ant build XML file and all Ant tasks can now be executed as Gradle tasks. We can automatically rename the Ant tasks if we want to avoid task name collisions with Gradle task names. We use a closure argument with the importBuild method and return the new task names. The existing Ant task name is the first argument of the closure.

Let's first create a simple Ant build.xml file:

00.<project>
01. 
02.<target name="showMessage"
03.description="Show simple message">
04. 
05.<echo message="Running Ant task 'showMessage'"/>
06. 
07.</target>
08. 
09.<target name="showAnotherMessage"
10.depends="showMessage"
11.description="Show another simple message">
12. 
13.<echo message="Running Ant task 'showAnotherMessage'"/>
14. 
15.</target>
16. 
17.</project>

The build file contains two targets: showMessage and showAnotherMessage with a task dependency. We have the next example Gradle build file to use these Ant tasks and prefix the original Ant task names with ant-:

00.// Import Ant build and
01.// prefix all task names with
02.// 'ant-'.
03.ant.importBuild('build.xml') { antTaskName ->
04."ant-${antTaskName}".toString()
05.}
06. 
07.// Set group property for all
08.// Ant tasks.
09.tasks.matching { task ->
10.task.name.startsWith('ant-')
11.}*.group = 'Ant'

We can run the tasks task to see if the Ant tasks are imported and renamed:

$ gradle tasks --all
...
Ant tasks
---------
ant-showAnotherMessage - Show another simple message [ant-showMessage]
ant-showMessage - Show simple message
...
$

We can execute the ant-showAnotherMessage task and we get the following output:

00.$ gradle ant-showAnotherMessage
01.:ant-showMessage
02.[ant:echo] Running Ant task 'showMessage'
03.:ant-showAnotherMessage
04.[ant:echo] Running Ant task 'showAnotherMessage'
05. 
06.BUILD SUCCESSFUL
07. 
08.Total time: 3.953 secs
09.$

Written with Gradle 2.2.1

Gradle Goodness: Rename Ant Task Names When Importing Ant Build File的更多相关文章

  1. Gradle Goodness: Task Output Annotations Create Directory Automatically

    Gradle Goodness: Task Output Annotations Create Directory Automatically One of the great features of ...

  2. Gradle Goodness: Check Task Dependencies With a Dry Run

    We can run a Gradle build without any of the task actions being executed. This is a so-called dry ru ...

  3. Gradle Goodness: Working with Live Task Collection

    Gradle support the definition of so called live collections. These collections are mostly created ba ...

  4. Gradle Goodness: Copy Files with Filtering

    Gradle Goodness: Copy Files with Filtering Gradle's copy task is very powerful and includes filterin ...

  5. Gradle Goodness: Renaming Files while Copying

    With the Gradle copy task we can define renaming rules for the files that are copied. We use the ren ...

  6. Gradle Goodness: Adding Tasks to a Predefined Group

    In Gradle we can group related tasks using the group property of a task. We provide the name of our ...

  7. Gradle Goodness: Init Script for Adding Extra Plugins to Existing Projects

    Gradle Goodness: Init Script for Adding Extra Plugins to Existing Projects Gradle is very flexible. ...

  8. Gradle学习之创建Task的方法

    请通过下面方式下载本系列文章的Github演示样例代码: git clone https://github.com/davenkin/gradle-learning.git     Gradle的Pr ...

  9. Gradle 使用教程之 Task 详解

    最近打算学习下 gradle 在 Android 中的使用,结果百度出来的文章都是介绍性文章,没啥干货.后来找到 gradle 官网教程,自己对着撸. Gradle 概述: Gradle 是一个基于 ...

随机推荐

  1. netlink+netfilter

    http://blog.chinaunix.net/uid-21768364-id-3618377.html

  2. 通过Roslyn构建自己的C#脚本

    通过Roslyn构建自己的C#脚本 在下一代的C#中,一个重要的特性就是"Compiler as a Service",简单的讲,就是就是将编译器开放为一种可在代码中调用的服务.最 ...

  3. Karaf 基于 osgi

    Karaf是Apache旗下的一个开源项目.Karaf同时也是一个基于OSGi的运行环境,Karaf提供了一个轻量级的OSGi容器,可以用于部署各种组件,应用程序.Karaf提供了很多特性用于帮助开发 ...

  4. iOS学习之Object-C语言字符串和数值

    一.使用苹果帮助文档      1.帮助文档的作用:帮助开发者快速了解系统类的功能.           1)苹果每次iOS版本的升级,都会添加或者更新大量的API,并提供相应的参考文档.       ...

  5. Qt---- 点击按钮调用另一个窗口Ui

    -------------------------------------------------- #include "subdialog.h" SubDialog::SubDi ...

  6. Binary Tree Zigzag Level Order Traversal

    Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...

  7. VBS数组函数学习实例分析

    Array 函数 返回包含数组的Variant. Array(arglist) 参数:arglist是赋给包含在Variant中的数组元素的值的列表(用逗号分隔).如果没有指定此参数,则将会创建零长度 ...

  8. BICEP单元测试计划-四则运算-测试

    一.6个值得测试的具体部位,他们能够提高你的测试技巧 Right-结果是否正确? B-是否所有的边界条件都是正确的? I-能查一下反向关联吗? C-能用其他手段交叉检查一下结果吗? E-你是否可以强制 ...

  9. CS小分队第一阶段冲刺站立会议(5月10日)

    昨日成果:完成了从excel表格导入名单,并且进行抽号的功能 遇到的困难: 1.Excel表格导入时由于版本不同,导致旧版本无法显示,后经修改初步解决 2.改程序无法在未安装office excel驱 ...

  10. (补)PSP三张表

    学生     司新红 日期  2014.3.14 教师  王建民 项目计划总结 编程 完善程序 测试程序 阅读书籍 日总计 周日 10:00-10:30 pm 0.5 周一 10:00-10:30 p ...