views:

625

answers:

4

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?

+1  A: 

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.

JacobM
A: 

OK, you're asking about conditionally referencing the properties file.

I would expect either of the examples you gave to work. In what way are they failing? Note that to get messages from failures of property files you need to set debug to verbose (this is a feature so that you can reference property files that may only be there sometimes without generating error messages).

JacobM
A: 

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.
Michael Rutherfurd
A: 

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"/>
Vladimir