使用不同的参数多次调用ant目标

时间:2014-08-22 13:44:41

标签: ant

在Ant中是否可以使用不同的参数多次调用同一目标?

我的命令如下所示:

ant unittest -Dproject='proj1' unittest -Dproject='proj2'

问题是unittest运行两次,但仅适用于proj2:

unittest:
    [echo] Executing unit test for project proj2

unittest:
    [echo] Executing unit test for project proj2

我知道我可以运行两个单独的ant命令,但这会导致单元测试报告文件出现其他问题。

1 个答案:

答案 0 :(得分:23)

你可以使用antcall任务添加另一个目标,使用不同的参数调用你的unittest目标两次,例如。

<project name="test" default="test">

    <target name="test">
        <antcall target="unittest">
            <param name="project" value="proj1"/>
        </antcall>
        <antcall target="unittest">
            <param name="project" value="proj2"/>
        </antcall>
    </target>

    <target name="unittest">
        <echo message="project=${project}"/>
    </target>

</project>

输出:

test:

unittest:
     [echo] project=proj1

unittest:
     [echo] project=proj2

BUILD SUCCESSFUL
Total time: 0 seconds

或者,您可以将unittest目标更改为macrodef

<project name="test" default="test">

    <target name="test">
        <unittest project="proj1"/>
        <unittest project="proj2"/>
    </target>

    <macrodef name="unittest">
        <attribute name="project"/>
        <sequential>
            <echo message="project=@{project}"/>
        </sequential>
    </macrodef>

</project>
相关问题