Using ant to detect os and set property

You need to set value "true" to the property for the if condition to work. See the code below:

<target name="checkos">
    <condition property="isWindows" value="true">
            <os family="windows" />
    </condition>

    <condition property="isLinux" value="true">
            <os family="unix" />
    </condition>
</target>

HTH, Hari


I used this script, and it worked well for me:

<project name="dir" basedir=".">

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

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

  <target name="setWindowsRoot" if="isWindows">
    <property name="root.dir" value="c:\tmp\" />
  </target>

  <target name="setUnixRoot" if="isUnix">
    <property name="root.dir" value="/i0/" />
  </target>

  <target name="test" depends="setWindowsRoot, setUnixRoot">
    <mkdir dir="${root.dir}" />
  </target>

</project>

Move your condition out of the <target />, as your target probably isn't invoked.

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

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

If you want to set that single property based on the OS, you can set it directly as well without the need of creating tasks:

<condition property="sw.root" value="c:\flag">
    <os family="windows" />
</condition>

<condition property="sw.root" value="/opt/flag">
        <os family="unix" />
</condition>

<property name="sw.root" value="/os/unknown/"/>

Tags:

Ant