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.

comments powered by Disqus