Implementing Conditional Logic
Problem
You want to selectively execute portions of a build based on conditional logic.
Solution
Use target
dependencies, properties, and the
if and unless attributes of the
<target> tag.
Discussion
Ant does not support true conditional logic, such as if/then/else.
You can, however, execute targets depending on the state of
properties. This target only executes if the
xalanInstalled property is set:
<target name="compile" if="xalanInstalled"> ... </target>
If the property is not set, the target is ignored. You can also specify that a target should execute unless a property is set:
<target name="installXalan" unless="xalanInstalled"> ... </target>
See Also
Recipe 3.15 shows how to abort the build if a
property is not set. This is a form of conditional logic that is
specific to the fail task. See the Ant
documentation for the condition task to learn how
to set properties based upon existence of files, classes, or other
resources.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access