Running different ANT tasks depending on the operating system

How to run a different series of tasks inside of a single target tag based on the operating system so that your build script will work on all operating systems.
There are three ways to do it:
1.) The first way is to have a different task for each operating system. You detect the OS outside your task in the global scope and then set up different tasks that run depending on the OS. That method is listed in the top code sample. But what if you don't want to clutter your tasks list and you just want to quickly switch the logic inside one specific task on just one or two statements? For that solution read on.
2.) In the second method listed in the bottom code sample, you will need to add to ANT the 'if', 'then' and 'else' tasks. You will need to download the latest ant-contrib.jar file from the web and add it to ANT, then you can use the following task definition to add it to your build.xml file. You may have to poke around the web a bit to figure out how to install this JAR file. Once you have done that you can use the example code below in the task named detectOS.
3.) You can set the 'os' attribute in many tags which will allow the tag to run based on the operating system. See the last code example of the three below for how to do this.


<?xml version="1.0" encoding="UTF-8"?>
<!--Use ANT to trigger different logic inside a task depending on what OS is being used.
This is useful if you just want to switch some of the logic inside one part of your task
depending on the OS without creating a whole other task-->
<project name="detectOS" default="showOS" >
<!--Include new library
If, then, else library for supporting tasks
on how to install see:
https://www.google.com/search?q=how+to+install+ant-contrib.jar-->
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<pathelement location="ant-contrib.jar"/>
</classpath>
</taskdef>
<task name="showOS">
<!--Create property based on OS-->
<condition property="IS_WINDOWS" value="true" else="false">
<os family="windows"/>
</condition>
<!--Switch statement based on OS-->
<if>
<equals arg1="${IS_WINDOWS}" arg2="true" />
<then>
<echo message="This is a windows system" />
</then>
<else>
<echo message="This is not a windows system" />
</else>
</if>
</task>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<!--Use ANT to trigger different tasks based on what OS is being used. See the code
snipit bellow if you don't want to create a new task just to switch some small piece
of logic depending on the operating system.-->
<project name="detectOS" default="showOS" >
<!--Create properties based on OS
conditions are like properties or variables that get set based on what is true
inside the conidtion tag. Because the condition tag is called as a child of the tag
project the IS_WINDOWS and IS_UNIX properties are in the global scope. This means
you can access these variables or properties from any task in your script. If you want
to create variables inside a task and not in the global scope use the example bellow
in the code snipit detectOSInsideTask-->
<condition property="IS_WINDOWS">
<os family="windows"/>
</condition>
<condition property="IS_UNIX">
<os family="unix"/>
</condition>
<!--Define operating system based tasks
When these targets get called it will only run if the variable IS_WINDOWS or
IS_UNIX is true-->
<target name="show_windows" if="IS_WINDOWS" >
<echo message="This is a Windows system " />
</target>
<target name="show_unix" if="IS_UNIX" >
<echo message="This is a Unix system " />
</target>
<!--Run switch based on OS
This task acts like a switch statement. It will try to run two tasks:
show_windows and show_unix but each task will only run if its 'if'
attibute is true. For example if IS_UNIX = true and IS_WINDOWS = false
then only antcall target="show_unix" will be run.-->
<target name="showOS" >
<antcall target="show_windows" />
<antcall target="show_unix" />
</target>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project name="detectOS" default="showOS" >
<!--this is provided here for completeness. Some tags have the OS attribute on them
which allows you to control whether the task will be ran based on the OS. I find it
most usefull with the exec tag since this tag talks to the operating system-->
<task name="showOS">
<exec executable="sh" os="Mac OS X">
<arg value="-c"/>
<arg value="echo 'This is a Mac OSX system';"/>
</exec>
<exec executable="echo" os="Windows 2000,Windows NT,Windows XP">
<arg value="This is a windows system"/>
</exec>
</task>
</project>