subant 任务对于您有多个共享相似结构的子项目的情况很有用。
在您的主 build.xml 中,定义一个在您的游戏子目录中摩擦所需构建目标的目标,以及所有通用构建目标。
<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>
然后,在 game-* 子目录中,创建一个简单的 build.xml 链接回通用文件。
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>
编辑:如果您的构建需要根据用户的输入或预定义的属性包含/排除某些子项目,您可以修改subant 任务的嵌套资源集合以适应此情况。
<property name="game.includes" value="game-*" />
<property name="game.excludes" value="" />
<subant target="deploy">
<dirset dir="." includes="${game.includes}" excludes="${game.excludes}" />
</subant>
然后,用户可以运行一个命令,该命令可选择传递game.includes 和/或game.excludes 的值。如果未指定这些属性,则上面由property 任务定义的值将用作默认值。