WellieeGee
WellieeGee

Reputation: 322

Ant: Referencing dynamic properties

I want to use the same ant script to do a build in either my local windows environment or on our redhat build server.

I have a variable 'buildDirectory' in two files (build_unix.properties & build_windows). I want to set variables depending on the environment.

<osfamily property="os.family"/>
<property file="./build_${os.family}.properties" />
<property name="tmp-base.folder" value="${buildDirectory}/tmp/"/>

I also tried

<if>
    <os family="unix"/>
    <then>
        <property file="./build_unix.properties" />
    </then>
    <else>
        <property file="./build_windows.properties" />
    </else>
</if>

Any ideas?

Upvotes: 0

Views: 3546

Answers (3)

Vladimir
Vladimir

Reputation: 6871

you can have two files : build-unix.properties and build-windows.properites and in your init target, you simply do a copy:

<copy file="build-${os.family}.properties" tofile="build.properties">

your main build.xml could simply reference the build.properties file.

This could be quite tricky to make it work properly so what I would do is to keep all properties of the build server in the build.properties file and allow local override (for your Windows box) in a build-local.properties. You can also move properties to a build-common.properties so that your build file would include the properties files in the following order

<property file="build-local.properties"/>
<property file="build-common.properties"/>
<property file="build.properties"/>

Upvotes: 0

Michael Rutherfurd
Michael Rutherfurd

Reputation: 14045

I would expect your if...then...else version to work. As it apparently isn't I would add some extra echo's to make sure your build is doing what you think it is doing.

  1. An echo inside the then and the else would let you know for certain what path is being executed.
  2. Add a prefix to the properties (e.g. <property file="..." prefix="test" />) and then add an <echoproperties prefix="test" /> to ensure the properties you think are being loaded are.

Upvotes: 0

Jacob Mattison
Jacob Mattison

Reputation: 51052

Are you asking how you can automatically set the os.family property in the first place?

If so, here's one approach:

  <available file="C:\\" property="os.family" value="windows" />
  <available file="/usr/bin/" property="os.family" value="unix" />

Ah! Edited question makes it more clear what you are asking (not this), and I see that you're using the "osfamily" task from ant-contrib to determine the OS family. But I'll leave this answer up for anyone who is just using ant without ant-contrib.

Upvotes: 1

Related Questions