ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From (Praveen Shenoy)
Subject Re: Conditional logic in ANT build scripts
Date Thu, 10 Apr 2003 16:59:54 GMT
An "if else" task would be of great use. Will Ant support anything like 
this in future versions? I have come across more than half a dozen 
situations writing our 3 dozen build xmls for our project. The way I 
generally try to get this to work is by having task "a" depend on task b 
and c and use an "if" clause in task b and "unless" clause in task c. 
The classic case I can tell you is as below:

We use a build.xml for running automated builds. We have wrapper shell 
and perl scripts that eventually call ant with a build.xml. We use one 
build.xml for doing both branch builds and tip builds. Tip builds are 
easy as we do not have to pass any tags to the cvs task. When it comes 
to branch builds, we need to pass a tag. Now the question is, if we had 
an if else task, this was pretty easy. Something like this
<if tag not null>
   <cvs ....... tag="cvstag">
   <cvs ......>

In the absence of this, what I did was, pass an additional isBranch 
property with the tag to the build.xml. So, the command line looks like this

ant -Dcvstag=branch_tag -DisBranch=true

and then I have this task

<target name="checkout" depends="checkout_notag, checkout_with_tag"/>
<target name="checkout_notag" unless="isBranch">
   <cvs .........>
<target name="checkout_with_tag" if="isBranch">

So, in yor case, if it s just two scenarios, you can do the same. This 
fails, when you have more than two conditions.

Andrew Zeon wrote:

>I know that the 'if' attribute in the 'target' task can be used to determine
>whether or not a target should be executed. However, it is my understanding
>that the 'if' attribute does not take into consideration the value of the
>property... it merely checks its presence. Is there a way I can dynamically
>determine whether or not a target is to be executed according to the actual
>value of a property?
>The reason why I ask is I have a build script, but that build script's
>behaviour has to be different for different environments. This is how I
>invoke my script:
>"ant -Denvironment=dev dist"
>for the development environment
>"ant -Denvironment=staging dist "
>for the staging environment
>ant -D environment=live dist
>for the live environment
>I have targets which should only be run for specific environments... how can
>I do this?
>To unsubscribe, e-mail:
>For additional commands, e-mail:

View raw message