Skip to content

Instantly share code, notes, and snippets.

@nicholasdunbar
Last active August 29, 2015 14:10
Show Gist options
  • Save nicholasdunbar/8ccb8e1ac798d6b3b8d8 to your computer and use it in GitHub Desktop.
Save nicholasdunbar/8ccb8e1ac798d6b3b8d8 to your computer and use it in GitHub Desktop.
If you want to run a different logics based on the operating system in your ANT build.xml file here are two ways to do it.
<?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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment