I need some help with using Ant's property files. I have the following:
A build.properties file. This file contains one piece of information: on=1
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?
The name of the project, as specified in the name attribute of the project element. The default target of the current project. Comma separated list of the targets that were invoked in the current project. The full location of the Ant jar file.
Ant uses an xml file for its configuration. The default file name is build. xml . Ant builds are based on three blocks: tasks, targets and extension points. A task is a unit of work which should be performed and constitutes of small atomic steps, for example compile source code or create Javadoc.
The <property> task is used to set the Ant properties. The property value is immutable, once the value is set you cannot change it. To set a property to a specific value you use Name/value assignment.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With