If and if in ant

I would like to clarify if and unless in ANT script

I have the following code:

 <condition property="hasExtensions"> <contains string="${Product_version}" substring="Extensions"> </condition> <exec executable="${TrueCM_App}\ssremove.exe" unless="hasExtensions"> ... </exec> 

Does this mean that ssremove.exe will execute above <exec> if Product_version does not contain the string "Extensions" ?

Then what about the opposite case: if it contains the string "Extensions" ? Will my code look like this:

 <condition property="hasExtensions"> <contains string="${Product_version}" substring="Extensions"> </condition> <!-- here below it does not have the string "Extensions" --> <exec executable="${TrueCM_App}\ssremove.exe" unless="hasExtensions"> ... </exec> <!-- below is for if it has the string "Extensions" --> <exec executable="${TrueCM_App}\ssremove.exe" if="hasExtensions"> ... </exec> 
+8
xml ant
source share
5 answers

You have the correct logic, but I'm not sure if the <exec> task accepts if and unless . See docs for more details.

You will probably have to wrap the <exec> tasks in a target that checks the condition. For example:

 <condition property="hasExtensions"> <contains string="${Product_version}" substring="Extensions"> </condition> <target name="ssremove" unless="hasExtensions"> <exec executable="${TrueCM_App}\ssremove.exe"> ... </exec> </target> 

Then if you run ant ssremove , I think you will get what you want.

+10
source share

"exec" does not support either if or if, so ChrisH's answer is correct. Usually, you also transfer the condition to the target and make it dependent on another target:

 <target name="-should-ssremove"> <condition ... </target> <target name="ssremove" depends="-should-ssremove" unless="hasExtensions"> ... 

Pay attention to the idiom of launching a target with a hyphen (-should-ssremove) to prevent its use from the command line. (You cannot make "ant -should-ssremove" because ant will consider it as an argument instead of a target - this is described in the ant manual)

Another smart idiom to use in this case, also from the manual, is to use the old "defined" if / except value and the new (starting with ant 1.8) extension and comparison with true / false.

This will give you:

 <target name="-should-ssremove" unless="hasExtensions"> <condition ... </target> <target name="ssremove" depends="-should-ssremove" unless="${hasExtensions}"> ... 

Note the difference: the first target uses the plain old, unless the second target extends the hasExtensions variable (using $ {}, which are not used in the first goal) and only works if it expands to true (which is the default value which is "available" will be installed on it, but you can set by adding the attribute "value" to "available")

The advantage of this idiom is that you can set the hasExtensions property from the outside, to a file that imports this (e.g. build.xml) or on the command line:

  ant -DhasExtensions=true ssremove 

This works because the -should-ssremove target will not be executed if hasExtensions is already defined (which up to 1.8 is the only logic supported by if / except). Therefore, your external definitions of trump cards - after all. On the other hand, the ssremove target will only work if the hasExtensions property is false. And this will always be determined by the validation time - thanks to the dependency on -should-ssremove.

+7
source share

Starting with Ant 1.9.1, you can add if and if attributes for all tasks and nested elements use special namespaces:

 xmlns:if="ant:if" xmlns:unless="ant:unless" 

See if and if

 <project name="tryit" xmlns:if="ant:if" xmlns:unless="ant:unless"> <condition property="onmac"> <os family="mac" /> </condition> <echo if:set="onmac">running on MacOS</echo> <echo unless:set="onmac">not running on MacOS</echo> </project> 

It also supports if: true / except: true and if: blank / except: blank.

+5
source share

Ant contrib also has an if task. I personally find it easier to read an ant script that uses an if task than those that use conditional goals. The if task is not trusted by the ant community, although you probably want to go with the ChrisH solution if you don't plan on doing a lot of conditional things.

+1
source share
 <condition property="myStatus" value="My test is OK" else="My test is KO"> <available file="${filePath}/${fileName}.txt"/> </condition> <echo message="${myStatus}" /> 
+1
source share

All Articles