Call ant target multiple times with different parameters

≯℡__Kan透↙ 提交于 2019-12-09 17:35:17

问题


Is it possible in Ant to call the same target multiple times with different parameters?

My command looks like the following:

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

The problem is that unittest gets run twice, but only for proj2:

unittest:
    [echo] Executing unit test for project proj2

unittest:
    [echo] Executing unit test for project proj2

I know that I can run two separate ant commands, but that is going to cause additional problems with the unit test report files.


回答1:


You could add another target to invoke your unittest target twice, with different parameters, using the antcall task e.g.

<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>

Output:

test:

unittest:
     [echo] project=proj1

unittest:
     [echo] project=proj2

BUILD SUCCESSFUL
Total time: 0 seconds

Alternatively, you could change the unittest target to be a 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>


来源:https://stackoverflow.com/questions/25448523/call-ant-target-multiple-times-with-different-parameters

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!