Using ant to detect os and set property
I want to set a property in an ant task differently by os type.
The property is a directory, in windows i want it to be "c:\flag" in unix/linux "/opt/flag".
My current script only works when i run it with the default target, but why ?
<target name="checksw_path" depends="if_windows, if_unix"/>
<target name="checkos">
<condition property="isWindows">
<os family="windows" />
</condition>
<condition property="isLinux">
<os family="unix" />
</condition>
</target>
<target name="if_windows" depends="checkos" if="isWindows">
<property name="sw.root" value="c:\flag" />
<echo message="${sw.root}"/>
</target>
<target name="if_unix" depends="checkos" if="isLinux">
<property name="sw.root" value="/opt/flag" />
<echo message="${sw.root}"/>
</target>
In all my ant targets i've added a "depends=checksw_path".
If i run the default target in windows i've got correctly "c:\flag" but if i run a non default target i've got that the debug goes in the if_windows but the instruction " " does not set the property that remains /opt/flag. I'm using ant 1.7.1.
Solution 1:
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>
Solution 2:
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
Solution 3:
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>
Solution 4:
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/"/>
Solution 5:
Try setting <sysproperty key="foobar" value="fowl"/>
in your java task.
Then, in your app, use System.getProperty("foobar");