How to check OS with Ant using an if-switch

As there is no built-in native if clause in Ant, you have to use this workaround. This "if" statement hack can be used to check for the operating system running on the respective machine. Unfortunately you cannot include a property file like

<property file="" />

into the respective "if" section which would affect the global scope of the Ant script. Instead this property file inclusion affects the local scope of the respective "if" path only.

<project name="OS-check-with-if-switch" default="chkOS">
<!-- affects global scope -->
<property file="" />
<target name="chkOS" description="if switch to set OS specivic settings">
<condition property="isUnix">
<os family="unix" />
<condition property="isWindows">
<os family="windows" />
<antcall target="setUnixEnv" />
<antcall target="setWindowsEnv" />
<target name="setUnixEnv" if="isUnix">
<echo>This is an Unix machine.</echo>
<!-- affects local scope only -->
<property file="" />
<target name="setWindowsEnv" if="isWindows">
<echo>This is a Windows machine.</echo>
<!-- affects local scope only -->
<property file="" />

The opposite of the "if" argument in the "target" tag is "unless".

If isUnix=true then
if="isUnix" target will be executed and
unless="isUnix" target will be not executed.


Popular posts from this blog

Tuning ext4 for performance with emphasis on SSD usage

NetBeans 6.1: Working with Google´s Android SDK, Groovy and Grails