Apache Ant target execute based on condition

July 21, 2011

To execute an Ant target based on condition, use the “if” attribute of the target,

<target name="build-module-A" if="module-A-present"/>

if the module-A-present property is set (to any value, e.g. false), the target will be run.


<target name="use-file" depends="-check-use-file" if="${file.exists}">

As of Ant 1.8.0, you may instead use property expansion; a value of true (or on or yes) will enable the item, while false (or off or no) will disable it. Other values are still assumed to be property names and so the item is enabled only if the named property is defined.