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="unix.properties" />

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


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

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

Comments

Popular posts from this blog

Apple's evaluating a new JVM

Tuning ext4 for performance with emphasis on SSD usage