Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 900
HELOX Avatar asked May 07 '15 07:05

HELOX


People also ask

How to use Gradle with Ant?

You could even use Gradle simply as a powerful Ant task scripting tool. Ant can be divided into two layers. The first layer is the Ant language. It provides the syntax for the build.xml file, the handling of the targets, special constructs like macrodefs, and so on. In other words, everything except the Ant tasks and types.

What is the default message priority for a Gradle task?

For example, there is no message priority that maps directly to the LIFECYCLE log level, which is the default for Gradle. Many Ant tasks log messages at the INFO priority, which means to expose those messages from Gradle, a build would have to be run with the log level set to INFO, potentially logging much more output than is desired.

How to migrate tasks to a Gradle build?

If you have such tasks, then there are two main options for migrating them to a Gradle build: The first option is usually quick and easy, but not always. And if you want to integrate the task into incremental build, you must use the incremental build runtime API.

How to use ant in your build script?

Using Ant tasks and types in your build In your build script, a property called ant is provided by Gradle. This is a reference to an AntBuilder instance. This AntBuilder is used to access Ant tasks, types and properties from your build script. There is a very simple mapping from Ant's build.xml format to Groovy, which is explained below.


Video Answer


2 Answers

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
like image 152
Oleg Pavliv Avatar answered Oct 06 '22 21:10

Oleg Pavliv


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'])
}
like image 21
Nikita Skvortsov Avatar answered Oct 06 '22 22:10

Nikita Skvortsov