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. python 实现求和、计数、最大最小值、平均值、中位数、标准偏差、百分比。

    import sys class Stats: def __init__(self, sequence): # sequence of numbers we will process # conver ...

  2. UITableView swift

    // // ViewController.swift // UILabelTest // // Created by mac on 15/6/23. // Copyright (c) 2015年 fa ...

  3. JBOSS和WebLogic区别

    JBoss: 1.  JBoss开放源代码Java EE实现,成本低,好控制. 2.  JBoss需要的内存和硬盘空间比较小,但是只适合做小项目. 3.  安装非常简单.先解压缩JBoss打包文件再配 ...

  4. hive搭建配置

    下载cd /data0/software/hivewget http://mirror.bit.edu.cn/apache/hive/hive-0.12.0/hive-0.12.0-bin.tar.g ...

  5. 如何维护SSH安全

    遇到两次,一次是公司服务器搭建好后,有人尝试ssh暴力破解,auth.log不停出现错误提示 还有买的米国vps,很荣幸地遭到来自波兰的ssh破解尝试 不得不重视ssh的安全 方法: 修改sshd_c ...

  6. C#委托详解(2):实现方式大全

    本系列文章将详细探讨C#中的委托,列举其主要的实现方式,并分析其在设计层面和编码层面带来的好处,最后会讨论其安全性和执行效率等. 接上篇(C#委托详解(1):什么是委托)介绍完什么是委托之后,来看看C ...

  7. 记codevs第一次月赛

    第一次参加这种有奖励的比赛(没错,我就是为猴子而去的 一年没怎么碰代码果然手生,还是用没写多久的C++,差点全跪了 T1数学奇才琪露诺: 首先定义一个函数F(x),F(x)=x的各个数位上的数字和 然 ...

  8. WebServices中Xml的序列化

    一.定义序列化实体类 [System.Xml.Serialization.XmlRoot("Custome_Xml_Root_Name")] //自定义生成的Xml根目录名称 pu ...

  9. 【BZOJ】【1050】【HAOI2006】旅行comf

    枚举/暴力/Kruskal orz……我sb了……其实是sb题<_< 有一道题问的是最小极差生成树……(不记得是什么名字了,就是求最大边权与最小边权差最小的生成树)做法是枚举最小边,然后k ...

  10. 【BZOJ】【1049】【HAOI2006】数字序列

    DP 第一问比较水……a[i]-=i 以后就变成最长不下降子序列问题了,第二问这个结论好神奇,考试的时候怎么破?大胆猜想,不用证明?TAT 题解:http://pan.baidu.com/share/ ...