Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call ant target multiple times with different parameters

Tags:

ant

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.

like image 561
JamesE Avatar asked Aug 22 '14 13:08

JamesE


People also ask

How do I run multiple targets in Ant?

You can specify multiple targets using nested <target> elements instead of using the target attribute. These will be executed as if Ant had been invoked with a single target whose dependencies are the targets so specified, in the order specified. The name of the called target.

How do I call Ant target from command line?

To run the ant build file, open up command prompt and navigate to the folder, where the build. xml resides, and then type ant info. You could also type ant instead. Both will work,because info is the default target in the build file.

How do Ant builds work?

Ant builds are based on three blocks: tasks, targets and extension points. A task is a unit of work which should be performed and constitutes of small atomic steps, for example compile source code or create Javadoc. Tasks can be grouped into targets. A target can be directly invoked via Ant.


1 Answers

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>
like image 135
sudocode Avatar answered Oct 20 '22 18:10

sudocode