Calling Ant Macrodef from Gradle

1.1k Views Asked by At

I can't seem to find a way to list and/or call an Ant Macrodef from within my Gradle script. The Gradle User Guide talks about Macrodefs but does not provide an example anywehere. Could anyone tell me how to accomplish this?

At the moment I import the Ant build by executing the ant.importBuild task. This works fine as the Ant targets show up as Gradle tasks. However, I am not able to list and/or call the Ant Macrodefs stated in the Ant build. Could anyone provide me the answer?

2

There are 2 best solutions below

3
Oleg Pavliv On BEST ANSWER

Your build.xml

<project name="test">

    <macrodef name="sayHello">
        <attribute name="name"/>
        <sequential>
            <echo message="hello @{name}" />
        </sequential>
    </macrodef>

</project>

and build.gradle

ant.importBuild 'build.xml'

task hello << {
      ant.sayHello(name: 'darling')
}

Let's test it

/cygdrive/c/temp/gradle>gradle hello
:hello
[ant:echo] hello darling

BUILD SUCCESSFUL

Total time: 2.487 secs
0
Nikita Skvortsov On

Ant allows macro names that do not fit into Groovy's identifier limitations. If this is the case, explicit invokeMethod call can help. Given:

<project name="test">

<macrodef name="sayHello-with-dashes">
    <attribute name="name"/>
    <sequential>
        <echo message="hello @{name}" />
    </sequential>
</macrodef>

</project>

this will work

ant.importBuild 'build.xml'

task hello << {
  ant.invokeMethod('sayHello-with-dashes', [name: 'darling'])
}