Ant FAQ: How to determine the platform operating system in an Ant build script

Problem

You're creating an Ant build script, and you need to determine the operating system the script is running on, so you can make conditional decisions within the build script. You typically want/need to do this if you're going to run tasks/targets that are different for each operating system (Mac, Windows, Unix, etc.).

Solution

First, create several small Ant condition tests that check the "os family". Here's a check I created to test for the Mac OS X operating system:

<condition property="isMac">
  <os family="mac" />
</condition>

The property created here named isMac will now hold a true/false boolean value that you can test with you Ant target later, like this:

<target name="doMac" if="isMac">
  <echo message="Came into the Mac target" />
  <!-- do whatever you want to do here for Mac systems -->
</target>

Now just perform this same condition/property/test sequence for the other operating systems you want to handle, including Windows and Unix systems.

The source code for my Ant build script

Here's the complete source code for an Ant build script that demonstrates this process of detecting the current operating system, and then conditionally performing certain operating system specific tasks:

<?xml version="1.0"?>

<!--
  Ant Ant build script that demonstrates how to test to see
  which operating system (computer platform) the Ant build
  script is currently running on. Currently tests for Mac OS X,
  Windows, and Unix systems.
  Created by Alvin Alexander, http://alvinalexander.com
-->

<project default="OS-TEST" name="Ant Operating System Test" >

  <!-- set the operating system test properties -->
  <condition property="isMac">
    <os family="mac" />
  </condition>

  <condition property="isWindows">
    <os family="windows" />
  </condition>

  <condition property="isUnix">
    <os family="unix" />
  </condition>

  <!-- define the operating system specific targets -->
  <target name="doMac" if="isMac">
    <echo message="Came into the Mac target" />
    <!-- do whatever you want to do here for Mac systems -->
  </target>

  <target name="doWindows" if="isWindows">
    <echo message="Came into the Windows target" />
  </target>

  <target name="doUnix" if="isUnix">
    <echo message="Came into the Unix target" />
  </target>

  <!-- define our main/default target -->
  <target name="OS-TEST" depends="doMac, doWindows, doUnix">
    <echo message="Running OS-TEST target" />
  </target>

</project>

The output from this Ant build script

When I run this Ant build script on my Mac OS X 10.5.7 system using Ant version 1.7.0, I get the following output:

Buildfile: build.xml

doMac:
     [echo] Came into the Mac target

doWindows:

doUnix:
     [echo] Came into the Unix target

OS-TEST:
     [echo] Running OS-TEST target

BUILD SUCCESSFUL
Total time: 0 seconds

As I mentioned in a previous blog entry, this build script responds to both the isMac and isUnix test properties, and I'm going to dig into this and see if I can understand why. (Mac OS X is build on top of a BSD/Mach base, so I'm assuming that's the answer, but I don't know for sure.)