Gradle Goodness: Rename Ant Task Names When Importing Ant Build File

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:



    <target name="showMessage"
        description="Show simple message">

        <echo message="Running Ant task 'showMessage'"/>


    <target name="showAnotherMessage"
        description="Show another simple message">

        <echo message="Running Ant task 'showAnotherMessage'"/>



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-:

// Import Ant build and 
// prefix all task names with
// 'ant-'.
ant.importBuild('build.xml') { antTaskName ->

// Set group property for all 
// Ant tasks.
tasks.matching { task ->'ant-')
}*.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:

$ gradle ant-showAnotherMessage
[ant:echo] Running Ant task 'showMessage'
[ant:echo] Running Ant task 'showAnotherMessage'


Total time: 3.953 secs

Written with Gradle 2.2.1

Reference: Gradle Goodness: Rename Ant Task Names When Importing Ant Build File from our JCG partner Hubert A. Klein Ikkink at the JDriven blog.

Hubert Ikkink

My name is Hubert A. Klein Ikkink also known as mrhaki. I work at the great IT company JDriven. Here I work on projects with Groovy & Grails, Gradle and Spring. At JDriven we focus on SpringSource technologies. All colleagues want to learn new technologies, support craftmanship and are very eager to learn. This is truly a great environment to work in.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button