Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect the windows OS in ANT

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.

like image 911
Nathan Avatar asked Mar 05 '12 12:03

Nathan


People also ask

What is the ant command?

It is the most complete Java build and deployment tool available. It is platform neutral and can handle platform specific properties, such as file separators. It can be used to perform platform specific tasks such as modifying the modified time of a file using 'touch' command. Ant scripts are written using plain XML.

How do I run an Ant script?

To run the ant build file, open up command prompt and navigate to the folder, where the build. xml resides, and then type ant info. You could also type ant instead. Both will work,because info is the default target in the build file.


1 Answers

It looks like the ANT <condition> can test for name, family & version of operating system:

  • Ant Tasks - conditions

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.

like image 84
Richard Le Mesurier Avatar answered Oct 26 '22 06:10

Richard Le Mesurier