antgradlebuild.gradlemacrodef

Calling Ant Macrodef from Gradle


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?


Solution

  • 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