How to detect the windows OS in ANT
It looks like the ANT <condition>
can test for name, family & version of operating system:
Based on that link, there are some properties related to OS that we can query. One is the normal family
property used in the common code:
<!-- CHECK FOR WINDOWS FAMILY OS -->
<condition property="is_windows">
<os family="windows"/>
</condition>
My version of ANT does not print out a resolved value for ${os.family}
.
There is also:
- os.name <--- this is the one you need to check
- os.arch
- os.version
Here's a demo script I made to show the use of these properties:
<?xml version="1.0" encoding="UTF-8"?>
<project name="Test" default="build" >
<!-- CHECK FOR WINDOWS FAMILY OS -->
<condition property="is_windows">
<os family="windows"/>
</condition>
<condition property="is_windows_7">
<os name="Windows 7"/>
</condition>
<!-- DISPLAYS WINDOWS OS -->
<target name="display_windows" if="is_windows" >
<echo message="OS Family is: Windows" />
</target>
<target name="build" >
<antcall target="display_windows" />
<echo message="OS Name is: ${os.name}" />
<echo message="OS Architecture is: ${os.arch}" />
<echo message="OS Version is: ${os.version}" />
</target>
</project>
Since answering this question, the code above has been promoted to our production build system, where it is providing shared functionality across Windows & Mac.
@thekbb made a good suggestion to remove the <antcall target="display_windows" />
, and update the target definition to depend on display_windows
as per the below code:
<target name="build" depends="display_windows">
<echo message="OS Name is: ${os.name}" />
<echo message="OS Architecture is: ${os.arch}" />
<echo message="OS Version is: ${os.version}" />
</target>
This based on the fact that antcall
launches a new instance of ant in a new JVM. Some users may find this optimisation easier to understand; others may want to do this for performance reasons.
Nathan
Java + C developer for Credit Suisse, Develop in my spare time in Android, Ruby, Bash, C, Java, HTML+CSS and Javascript.
Updated on June 11, 2022Comments
-
Nathan almost 2 years
I'm using ant to compile a Java application. The problem is some of the devs are on win 7 and others are on xp and vista. Part of the compiling is to build an msi using WIX, on win7 this is one directory and on xp and vista it's in another.
The ant task is controlled in Maven. I'm after a way of telling the difference between windows os's in ant with a conditional tag to set the wix directory. Any ideas?
I know it will be in this format:
<if> <condition property="isWin7"> Check for windows 7 </condition> <then> set wix path to win 7 installation </then> <else> set to vista/xp wix installation </else> </if>
Any help would be great.
-
thekbb over 10 yearsIf you copy paste/this, be sure to remove the <antcall /> and change the build target to depend on the display windows target. antcall spins up a new instance of ant in a new jvm to echo that it's windows.
-
Richard Le Mesurier over 10 years@thekbb Thx for the tip. Why is it a bad thing to spin off a new copy in this example? What is a better way to fake a method call in ant?
-
thekbb over 10 yearsIt's largely stylistic - I've edited your answer to show what I was recommending. edit back if you disagree. When build files get largish and there are antcalls sprinkled about it's hard to get a picture of how control flows between targets. on modern hardware the cost to spin up a new version of ant is negligible, I ran each way on my mac and the difference was ~0.5 seconds
-
Richard Le Mesurier over 10 yearsupdated question to include both versions, including your explanation from your comment. Thx again.
-
N D over 9 yearsHere is a good article that details all the different ways you can trigger logic dependent on the Operating System. boulderapps.co/…