25

I need some help with using Ant's property files. I have the following:

  1. A build.properties file. This file contains one piece of information: on=1

  2. An ant.xml file. This file contains my build instructions.

I want to read the on attribute from my properties file and if the value is 1 I want to execute a task in my build file. Otherwise I want it to do nothing. Can anyone guide me on how to accomplish this?

Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
user842650
  • 251
  • 1
  • 3
  • 3
  • What do you mean, you want to do something? im assuming something with ant.xml? – RMT Jul 13 '11 at 12:48

4 Answers4

24

This should be all you need to do:

1.Grab the latest version of ant-contrib JAR and place in lib folder of your Ant installation.

2.Include your properties in your build script

<property file="build.properties"/>

3.Add the following taskdef entry to your build script

<taskdef resource="net/sf/antcontrib/antcontrib.properties"/>

4.And then finally, define an if task like so:

<if>
 <equals arg1="${on}" arg2="1" />
 <then>
   <echo message="I am going to do something here" />
 </then>
 <else>
   <echo message="I am going to do nothing" />
 </else>
</if>

Note that you can prepend an identifier to properties you import from property files. So for example you could do your import like so:

<property file="build.properties" prefix="uniqueprefix"/>

And then you would reference in your file, 'uniqueprefix.on', instead of just 'on'.

<equals arg1="${uniqueprefix.on}" arg2="1" />

You can use the built-in conditional task from Ant, but I have a feeling that if you need it you are better off with the extra functions that ant-contrib brings to the table. Also, note that its standard to name your build file as 'build.xml', and not 'ant.xml'. As it is, Ant will not be able to automatically locate it, given the name you have used. Good luck.

Perception
  • 79,279
  • 19
  • 185
  • 195
  • +1 and I'd accept it, although - with this solution we have to be careful an should choose an special namespace for those external properties - otherwise we might overwrite existing properties accidently. Just imagine, property `on` was defined in `build.xml` too... – Andreas Dolk Jul 13 '11 at 14:07
  • 1
    @Andreas_D, its actually possible to append a unique identifier to all properties included via property file. I will update my answer to include this information. – Perception Jul 13 '11 at 14:48
5

If you don't want to write your own Ant Task or use other libs, just "clean" ant, have a look at this one:

mybuild.properties:

on=on

Use on or true or something like that, 1 will not work.

build.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project name="project" default="default">
    <property file="mybuild.properties"/>    

    <target name="default" depends="on, off" description="description">
        <echo>default</echo>
    </target>

    <target name="on" if="${on}">
      <echo>on</echo>
    </target>

    <target name="off" unless="${on}">
      <echo>off</echo>
    </target>
</project>
Daniel
  • 3,019
  • 1
  • 20
  • 21
4

Seems to me that you want to implement something like below given task.

<property file="build.properties" />



<target name="default" description="Homeworks">
    <condition property="on">
        <equals arg1="{on}" arg2="1" />
    </condition>
    <antcall target="taska" />
    <antcall target="taskb" />
</target>

<target name="taska" if="on">
    <echo message="Testing task one" />
</target>
<target name="taskb" unless="on">
    <echo message="Testing task two" />
</target>

Let me know if you want a explanation, in details.

Vijay Shanker Dubey
  • 4,308
  • 6
  • 32
  • 49
  • thx very much. But i have still problem. value "on" ​​from a properties in general is not taken into consideration. if I change "on" to "off" ant call taskb, but if I assign to "on" any number- taska always run. – user842650 Jul 13 '11 at 13:43
0

An approach, that looks difficult but is, in fact, pretty easy: write a custom ant task (one simple java class, <20 lines of code). The task will

  1. read the properties file (location/name can be passed as an attribute to the task)
  2. assign the value of on to an ant property

Then you can use that ant property for your flow control.

public class MyOwnTask extends Task {

  private String filename = "build.properties"; // some default value

  public void setFilename(String filename) {
    this.filename = filename;
  }

  public void execute() {   // the "main" method
    Properties p = new Properties();
    p.load(filename);
    String onValue = p.get("on");
    getProject().setProperty("ON_PROPERTY", onValue);
  }
}

Then you need some <taskdef> and that's it.

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
  • Why would you need to do this when you can import a properties file in an Ant script? – Perception Jul 13 '11 at 13:00
  • 1
    @Perception - why don't you make an answer from this valuable information? Custom ant tasks is definitely more fun :) – Andreas Dolk Jul 13 '11 at 13:08
  • Even more fun is writing your own build harness for Ant, utilizing its Java API. I've submitted an answer, as humbly requested :-) – Perception Jul 13 '11 at 13:14