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. Client–server model

    Client–server model From Wikipedia, the free encyclopedia The client–server model of computing ] Oft ...

  2. Menu MenuItem

    Menu & MenuItem learning note Menu MenuItem MSDN Sample Code <Menu Grid.Row="0" Hor ...

  3. STM32管教复用与重映射关系

    摘自:http://blog.csdn.net/lincheng15/article/details/51789093 概括一下:复用就是一个引脚有几个功能,1.做普通IO输入输出 2.其他外设的输入 ...

  4. 0-N背包为题(动态规划算法)

    /****************0-N背包问题****************** * 有n个物体装入容量为c的背包,每一个物体有一个体积 * 和一个价值,所装入的物体体积之和不大于背包体积, * ...

  5. 【BZOJ 2324】 [ZJOI2011]营救皮卡丘

    Description 皮卡丘被火箭队用邪恶的计谋抢走了!这三个坏家伙还给小智留下了赤果果的挑衅!为了皮卡丘,也为了正义,小智和他的朋友们义不容辞的踏上了营救皮卡丘的道路. 火箭队一共有N个据点,据点 ...

  6. vs2013中把解决方案上传到SVN服务器

    在VS2013中直接上传代码到SVN服务器,在这之前,必须是你的电脑已经安装了TortoiseSVN. 其次,VS2013必须安装AnkhSVN插件.然后才可以向我下面所述一样使用TortoiseSV ...

  7. C#中如何利用操作符重载和转换操作符

    操作符重载 有的编程语言允许一个类型定义操作符应该如何操作类型的实例,比如string类型和int类型都重载了(==)和(+)等操作符,当编译器发现两个int类型的实例使用+操作符的时候,编译器会生成 ...

  8. VMWare Workstation 11安装CentOS7,附图 [原创]

    1.新建虚拟机 2.新建虚拟机向导,选择典型 3.选择稍后安装操作系统 4.选择linux版本,注意:宿主系统是64位的,此处就得选64位:宿主系统是32位的,此处就得选32位 5.选择路径 6.指定 ...

  9. vtune 不能查看pg的源代码

    在编译的时候,要加enable-debug 选项 并且如果之前有没有加的话,一定要make clean

  10. 或许你不知道(2):LinkedList

    一,基本的存储结构及数据存取 LinkedList与ArrayList同属List的范畴,ArrayList实现了RandomAccess接口,通过索引随机访问效率较高,而LinkedList提供了直 ...