6

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.

rgettman
  • 176,041
  • 30
  • 275
  • 357
Nathan
  • 187
  • 1
  • 3
  • 10
  • possible duplicate of [Using ant to detect os and set property](http://stackoverflow.com/questions/453170/using-ant-to-detect-os-and-set-property) – oers Mar 05 '12 at 14:25
  • I'd already checked that but what i want to do is check wether it's xp, vista or win7 the other option will only let you know if its windows. – Nathan Mar 07 '12 at 16:15
  • @Nathan - did you have any luck using my answer? Pls mark as accepted, or drop me a comment so I can help more. This technique is used in our production build system, but it may not solve *your* problem. – Richard Le Mesurier Jul 01 '13 at 09:54

1 Answers1

16

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.

Community
  • 1
  • 1
Richard Le Mesurier
  • 29,432
  • 22
  • 140
  • 255
  • 1
    If you copy paste/this, be sure to remove the 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. – thekbb Oct 15 '13 at 18:26
  • @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? – Richard Le Mesurier Oct 15 '13 at 18:55
  • 1
    It'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 – thekbb Oct 15 '13 at 19:21
  • updated question to include both versions, including your explanation from your comment. Thx again. – Richard Le Mesurier Oct 16 '13 at 11:09
  • Here is a good article that details all the different ways you can trigger logic dependent on the Operating System. http://boulderapps.co/running-different-ant-tasks-depending-on-the-operating-system – N D Nov 23 '14 at 01:15