5

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?

HELOX
  • 529
  • 2
  • 13

2 Answers2

5

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
Opal
  • 81,889
  • 28
  • 189
  • 210
Oleg Pavliv
  • 20,462
  • 7
  • 59
  • 75
  • Although this solution does not entirely solve my problem, this is the/a way to call Ant macrodefs within Gradle. The reason I couldn't find a way to call the macrodefs was that my macrodef name contains a few '-' characters, which Gradle can not cope with. – HELOX May 08 '15 at 11:00
  • Do you know a way to list all loaded macrodefs? – HELOX May 08 '15 at 11:01
  • @HELOX No, I didn't find a way to list them – Oleg Pavliv May 08 '15 at 12:08
2

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'])
}
Nikita Skvortsov
  • 4,768
  • 24
  • 37