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.
Get Java Extreme Programming Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.