从 Gradle 调用 Ant Macrodef

Calling Ant Macrodef from Gradle

我似乎找不到从我的 Gradle 脚本中列出 and/or 调用 Ant Macrodef 的方法。 Gradle 用户指南 talks 关于 Macrodefs 但没有提供任何示例。谁能告诉我如何做到这一点?

目前我通过执行 ant.importBuild 任务导入 Ant 构建。这很好用,因为 Ant 目标显示为 Gradle 任务。但是,我无法列出 and/or 调用 Ant 构建中声明的 Ant Macrodefs。谁能给我答案?

你的build.xml

<project name="test">

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

</project>

build.gradle

ant.importBuild 'build.xml'

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

我们来测试一下

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

BUILD SUCCESSFUL

Total time: 2.487 secs

Ant 允许使用不符合 Groovy 标识符限制的宏名称。 如果是这种情况,显式 invokeMethod 调用会有所帮助。 给定:

<project name="test">

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

</project>

这会起作用

ant.importBuild 'build.xml'

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