Search

September 26, 2012

OS-specific ANT properties

The ANT build tool for Java does a pretty decent job of abstracting away OS concerns from your build script. E.g., file paths can always be represented using the / separator and there are tasks for all the typical file system and build operations.

However, once in while you may find yourself in a situation where you need ANT to behave differently based on the operating system. In my case, I needed to specify path to the dot executable within graphviz, a graph drawing tool used by the Hibernate tools ANT package. Rather than torture my environment, I figured I would set a property based on the OS:

<target name="schema-doc">
    <property name="hibernate.tool.dot.options"
              value="-Gsplines=true -Edecorate" />
    <condition property="hibernate.tool.dot.executable" value="/usr/bin/fdp">
        <os family="unix" />
    </condition>
    <condition property="hibernate.tool.dot.executable"
               value="C:/graphviz/bin/fdp.exe">
        <os family="windows" />
    </condition>
    <mkdir dir="${hibernate.tool.target.dir}/doc" />
    <delete>
        <fileset dir="${hibernate.tool.target.dir}/doc" />
    </delete>
    <hibernatetool destdir="${hibernate.tool.target.dir}/doc">
        <configuration configurationfile="${basedir}/hibernate-tool.cfg.xml">
            <fileset dir="${src}" includes="**/*.hbm.xml" />
        </configuration>
        <classpath refid="hibernate.classpath" />
        <hbm2doc>
            <property key="dot.executable"
                      value="${hibernate.tool.dot.executable} ${hibernate.tool.dot.options}" />
        </hbm2doc>
    </hibernatetool>
</target>

The key portion here occurs near the top, using the <condition> directive. Here I’ve placed in inside the <target>, but you can use it outside of a <target> as well. The <os> element within the <condition> allows you test based on properties of the underlying operating system. I’ve chosen to base the test on family, but there are also name, version and arch tests as well.

(As a bonus tip here, I’ve also shown you how to pass extra arguments to graphviz when running it within Hibernate Tools.)

Now this is all well and good for one property, which is the situation I was dealing with, but what if you have a whole mess of properties to deal with? Making multiple <condition> tags for each property and OS combination will get old real fast. In that case, we use the built-in properties ANT supplies:

<property file="build-${os.name}-${os.version}-${os.arch}.properties" />
<property file="build-${os.name}-${os.version}.properties" />
<property file="build-${os.name}.properties" />
<property file="build.properties" />

Note the order here. Recall that once a property is defined within ANT it cannot be changed. So put the defaults in build.properties and then override them in the more specific properties files that are loaded first. Of course, you may not need to go all the way to the OS architecture level, but now you know how.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.