Using if:set, unless:set etc. to execute a task conditionally in Ant

I’m not sure anyone except me is still struggling on with using Ant, but just in case, here is a nice thing.

In Ant 1.9.1 they added a useful feature: instead of needing to use the <if> tag and similar from ant-contrib, you can conditionally execute “any” task.

(In fact, this appears to mean “any task except a macrodef” – see my post Using if:set, unless:set etc. with macrodefs in Ant.)

You need to add these namespaces at the top of your project:


and then make any task conditional by adding one of these attributes:


if:set and unless:set take a property name and run the task if it is set at all (or unless it is set at all).

<mytask if:set="" other_attrs="" .../>

Where mytask is the name of any ordinary Ant task like echo, jar etc.

(Note there is no ${} around the property name for if:set.)

if:true and unless:true take a value and run the task if it is true (or unless it is true), so they need to be used like this:

<mytask if:true="${property}" other_attrs="" .../>

(Note the ${} here, but not above.)

Similarly, if:blank and unless:blank take a value and run the task if it is blank (or unless it is blank), e.g.:

<mytask if:blank="${property}" other_attrs="" .../>

NOTE: The example in the documentation is wrong (at this time, 2013-09-13) – it uses the property name, but this does not work – you must surround it with ${} to get its value.

The properties can be specified in your build file as normal, or supplied on the command line to ant with

Here’s an example of using set:



    <property name="setinxml" value="true"/>

    <target name="build">
        <echo if:set="setinxml"     message="if:set=setinxml"/>
        <echo unless:set="setinxml" message="unless:set=setinxml"/>
        <echo if:set="notset"       message="if:set=notset"/>
        <echo unless:set="notset"   message="unless:set=notset"/>
        <echo if:set="setincmd"     message="if:set=setincmd"/>
        <echo unless:set="setincmd" message="unless:set=setincmd"/>


And here’s the output:

$ ant -version
Apache Ant(TM) version 1.9.2 compiled on July 8 2013
$ ant -Dsetincmd=true
Buildfile: build.xml

     [echo] if:set=setinxml
     [echo] unless:set=notset
     [echo] if:set=setincmd

Total time: 0 seconds

The documentation for this, such as it is, is here: If And Unless.

8 thoughts on “Using if:set, unless:set etc. to execute a task conditionally in Ant”

  1. Hi andy, thanks for this. I have three targets in one project in my ant script. and i just want to add an if statement (effectively) which control all the three targets. so for example if i set that variable to true, then all three targets should execute. if i set it to false, nothing should execute.Can i do something like that?

    str of my file: very simple:

    The above is how it looks, i just want to conditionally either execute them all or not execute any. Is there any way out ???

    I am so grateful for your response again, thanks very much

  2. Hi Priyasha, inside each of your targets you could have a <sequential> block, with if:set attributes for the variable you want to use to control them. Then they would execute, but actually do nothing. Hopefully that will work for what you want.

  3. Hey, thanks a lot, could you give me a small example. I am very new to ant and having a big hard time with this. Thanks, I really appreciate your help

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.