rewb0rn
rewb0rn

Reputation: 118

Execute selected ant tasks for a selected set of projects

I have defined targets in ant for our games, e.g. clean, build-ios, build-android, deploy-ios, deploy-android, etc. Now I would like to define a new set of targets that represent our games, say game1, game2, game3.

My goal is to be able to launch ant with a set of target games and a set of target tasks so that for each selected game, each selected task is executed.

Example pseudo code: Foreach [game1, game3]: clean, build-ios, deploy-ios

How can I achieve this with ant? A requirement would be to define which games and which tasks are selected through targets, not to write them in a file that is altered manually.

Upvotes: 0

Views: 46

Answers (1)

CAustin
CAustin

Reputation: 4614

The subant task is useful for situations where you have multiple subprojects that share similar structures.

In your main build.xml, define a target that rubs the desired build target on your game subdirectories, along with all of the generalized build targets.

<target name="deploy-all">
    <subant target="deploy">
        <dirset dir="." includes="game-*" />
    </subant>

    <echo message="All games deployed." />
</target>

<target name="deploy" depends="deploy-ios,deploy-android">
    <echo message="${ant.project.name} build complete." />
</target>

<target name="clean">
    <echo message="Cleaning ${ant.project.name}" />
</target>

<target name="build-ios" depends="clean">
    <echo message="Building iOS ${ant.project.name}" />
</target>

<target name="build-android" depends="clean">
    <echo message="Building Android ${ant.project.name}" />
</target>

<target name="deploy-ios" depends="build-ios">
    <echo message="Deploying iOS ${ant.project.name}" />
</target>

<target name="deploy-android" depends="build-android">
    <echo message="Deploying Android ${ant.project.name}" />
</target>

Then, in the game-* subdirectories, create a simple build.xml that links back to the common one.

game-1/build.xml:

<project name="game-1" default="build">
    <import file="../build.xml" />

    <echo message="=== Building Game 1 ===" />
</project>

game-2/build.xml:

<project name="game-2" default="build">
    <import file="../build.xml" />

    <echo message="=== Building Game 2 ===" />
</project>

Edit: If your build needs to include/exclude certain subprojects based on the user's input or a pre-defined property, you can modify the subant task's nested resource collection to accommodate for this.

    <property name="game.includes" value="game-*" />
    <property name="game.excludes" value="" />

    <subant target="deploy">
        <dirset dir="." includes="${game.includes}" excludes="${game.excludes}" />
    </subant>

The user can then run a command that optionally passes values for game.includes and/or game.excludes. If these properties are not specified, the values defined above by the property task will be used as defaults.

Upvotes: 1

Related Questions