Apache Ant If and Unless

Ant if and unless both are the attributes of <target> element (tasks). These attributes are used to control tasks whether task is run or not.

Apart from target, it can also be used with <target> and <junit> elements.

In earlier versions and Ant 1.7.1, these attributes are only property names. If the property is defined, it runs even the value is false.

For example there is no way to stop execution even after passing false.

// build.xml

Output:

No Arguments: Run it by without command line arguments. Just enter ant to the terminal, but first locate to the project location and it will show empty output.

With Argument: Now pass argument butfalse.

Ant -0Dfile.exists=false


Apache Ant If and Unless

Now pass argument but true.
Ant -Dfile.exists=true


Apache Ant If and Unless

Since Ant 1.8.0, we can use property expansion that allows to execute only if value is true. In new version, it gives us more flexibility and now we can override the condition value from the command line. See an example below.

// build.xml

Output:

No Arguments: Run it without command line arguments. Just enter ant to the terminal, but first locate to the project's location and it will show the empty output.

With Argument: Now pass argument but false.
Ant -Dfile.exists=false

No output, because this time if is not executed.

With Argument: Now pass argument but true. Now it shows output because if is evaluated.
Ant -Dfile.exists=true


Apache Ant If and Unless




Latest Courses