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. Window7上搭建symfony开发环境(PEAR)

    http://blog.csdn.net/kunshan_shenbin/article/details/7162243 1. 更新PEAR 进入PHP所在目录,找到go-pear.bat并双击. 一 ...

  2. 自学asp.net mvc(三)

    1.将前台框架的登录页面代码,复制到Login.cshtml. 2.将文本框替换. 3.缓存机制. 4.类图

  3. MongoDB学习笔记-数据库命令

    概念 数据库命令(database command)是一种非常特殊类型的查询.文档的创建.更新.删除及查询都属于数据库命令的范畴,它还包含管理性的任务(比如关闭服务器和克隆数据库).统计数据及执行聚合 ...

  4. svn merge和branch

    http://www.cnblogs.com/cxd4321/archive/2012/07/12/2588110.html 使用svn几年了,一直对分支和合并敬而远之,一来是因为分支的管理不该我操心 ...

  5. XAML中ContentControl,ItemsControl,DataTemplate之间的联系和区别

    接触XAML很久了,但一直没有深入学习.今天学习了如标题所示的内容,所以来和大家分享一下,或者准确的说是自我回顾一遍. 在XAML中,有两类我们常见的控件,分别是ContentControl和Item ...

  6. 快速排序QuickSort

    前几天实现了直接插入排序.冒泡排序和直接选择排序这三个基础排序.今天看了一下冒泡排序的改进算法,快速排序.单独记录一下,后面还有归并和基数排序等 快速排序 1.选择一个支点默认为数组第一个元素及arr ...

  7. mysql几个命令

    1.格式化输出 select * from mysql.user\G 2.显示版本 show version() 3.显示引擎 show engines mysql> show engines; ...

  8. php编写验证码

    今天学习到了php登录时的验证码,验证码在我们平时的网站建设中是非常重要的,对于放置一些灌水机.脚本攻击是一个很好地策略. 下面是我写的代码: <?php session_start(); // ...

  9. ubuntu(Eclipse+JDK) 自动安装脚本

    sudo rm -rf jdk1.8.0_40sudo rm -rf /usr/lib/jvm sudo tar -zxvf jdk-8u40-linux-i586.tar.gzsudo mkdir ...

  10. 对MVC的理解

    摘要:本文主要谈到了对PHP开发中MVC开发模式的理解. 当用户通过url触发命令时,例如url=http://control.blog.sina.com.cn/admin/article/artic ...