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. 关于asp.net和iis的进程/线程问题,假如网站有1000个人访问,会产生多少个进程/线程啊

    详解 ASP.NET异步   超好的文章

  2. linux命令行下使用R语言绘图

    系统:centos 6.4 64bit 环境安装参考:http://hi.baidu.com/solohac/item/4a18e78f1bef9b5825ebd99c 在R语言中可以使用png()等 ...

  3. node.js的学习

    require('http') 内置模块 server.js var http = require('http'); function start(){ server = http.createSer ...

  4. Entity Framework 学习第一天

    文章是作为初学者记录之用,没有学习过的同学可以借鉴一下,至于用过和高手嘛,就算了吧.仅是入门.废话不多说了,马上新建个项目,添加Entity Framework,这个词以下将用EF代替. 本文使用的I ...

  5. ios 唯一标示符

    大家知道苹果每部 iOS 设备都有一个 UDID,它就像设备的身份证一样,记录着设备的名称.类型甚至一些关于用户的私人信息.通常情况下,UDID 的一个最大功能就是帮助广告发布商向特定用户推送定向广告 ...

  6. [磁盘管理与分区]——关于分区、磁盘分区表、MBR

    磁盘连接与设备文件名的关系 1. 如下图所示:

  7. linux matplotlib入门

    python linux matplotlib 安装:   sudo apt-get install python-numpy 必须 先安装numpy matplotlib 安装:   sudo ap ...

  8. 随笔 planetest

    Camera跟随物体: import Scripts包,Component中的camera control会有smooth follow脚本,添加到Main Camera中,在脚本的target属性中 ...

  9. IOC框架的认识

    转:http://blog.csdn.net/wanghao72214/article/details/3969594 1 IoC理论的背景 我们都知道,在采用面向对象方法设计的软件系统中,它的底层实 ...

  10. Careercup - Microsoft面试题 - 5684901156225024

    2014-05-10 23:45 题目链接 原题: Arrange the numbers in an array in alternating order. For example if the a ...