Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading from .properties file in Ant build.xml

Tags:

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?

like image 224
user842650 Avatar asked Jul 13 '11 12:07

user842650


People also ask

How do I use Ant properties file?

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.

What is Ant build xml?

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.

Where are Ant properties set?

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.


2 Answers

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.

like image 132
Perception Avatar answered Sep 20 '22 06:09

Perception


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.

like image 26
Vijay Shanker Dubey Avatar answered Sep 20 '22 06:09

Vijay Shanker Dubey