How to build an ant target conditionally on Snow Leopard

844 Views Asked by At

I have a cross-platform application and we use ant to build different things on different platforms. Now a new requirement came up and I need to do things differently if building on Snow Leopard or later vs Leopard.

I've looked at http://www.devdaily.com/blog/post/java/how-determine-operating-system-os-ant-build-script which shows how to distinguish between Windows and Macintosh etc., and http://www.jajakarta.org/ant/ant-1.6.1/docs/en/manual/api/org/apache/tools/ant/taskdefs/condition/Os.html which shows additional properties for os, like ${os.version}.

What I haven't figured out is how can I compare the os.version value and if it is 10.6 or higher do the Snow Leopard thing. If I could set a variable snow_leopard to 1 when on Snow Leopard I think I would be able to figure the rest of it out.

2

There are 2 best solutions below

1
martin clayton On BEST ANSWER

You might use the condition task for this. The available conditions, notable for os are here.

It would work in the same way as for 'os family':

<condition property="isSnowLeopard">
    <os family="mac" version="10.6.6" />
</condition>

But that means you have to put in the incremental version number - the version string has to match exactly.

For a 'fuzzier' alternative, you could use a matches condition, something like this perhaps

<condition property="isSnowLeopard">
    <matches string="${os.version}" pattern="^10.6." />
</condition>

When OSX Lion emerges, you may want to extend the pattern like the this:

<condition property="isSnowLeopardOrGreater">
    <matches string="${os.version}" pattern="^10.[67]." />
</condition>

Or introduce a separate check for 10.7.

1
Raghuram On

Using the <if> task provided by ant-contrib, you can achieve this to an extent, by making an equals check for the os version.

...
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
    <classpath>
        <pathelement location="/location/of/ant-contrib-1.0b3.jar"/>
    </classpath>
</taskdef>
<target name="oscheck">
    <property name="osver" value="${os.version}"/>
    <if>
        <equals arg1="${os.version}" arg2="6.1"/>
        <then>
            <echo message="Windows 7"/>
            ...
        </then>
    </if>
</target>
...