Is there an if/else condition that I can use for an Ant task?
This is what i have written so far:
<target name="prepare-copy" description="copy file based on condition">
<echo>Get file based on condition</echo>
<copy file="${some.dir}/true" todir="." if="true"/>
</target>
The script above will copy the file if a condition is true. What if the condition is false and I wish to copy another file? Is that possible in Ant?
I could pass a param to the above task and make sure the param that's passed is
You can also do this with ant contrib's if task.
Since ant 1.9.1 you can use a if:set condition : https://ant.apache.org/manual/ifunless.html
The
if
attribute does not exist for<copy>
. It should be applied to the<target>
.Below is an example of how you can use the
depends
attribute of a target and theif
andunless
attributes to control execution of dependent targets. Only one of the two should execute.If you are using ANT 1.8+, then you can use property expansion and it will evaluate the value of the property to determine the boolean value. So, you could use
if="${copy-condition}"
instead ofif="copy-condition"
.In ANT 1.7.1 and earlier, you specify the name of the property. If the property is defined and has any value (even an empty string), then it will evaluate to true.
In my Case
DPARAM_BUILD_TYPE=Debug
if it is supplied than, I need to build for for Debug otherwise i need to go for building Release build. I write like above condition it worked and i have tested as below it is working fine for me.And property
${build.type}
we can pass this to other target or macrodef for processing which i am doing in my other ant macrodef.It work for me to implement condition so posted hope it will helpful.
The quirky syntax using conditions on the target (described by Mads) is the only supported way to perform conditional execution in core ANT.
ANT is not a programming language and when things get complicated I choose to embed a script within my build as follows:
ANT supports several languages (See script task), my preference is Groovy because of it's terse syntax and because it plays so well with the build.
Apologies, David I am not a fan of ant-contrib.