Tuesday, 23 August 2011

Writing to a properties file with Ant

I find this useful when I want to change how the application works depending on where and why it is being deployed. This blog post assumes you have Ant installed!

In the build.xml file;

<!-- Add the property file -->
<property file="project.properties"/>

<!--  Write a property to the file with a value based on the target -->
<target name="liveExample">
     <echo message="This target example has the live properties"/>
     <propertyfile
          file="project.properties"
          comment="Setting important system procedure variable to 100">
          <entry key="importantVariable" value="100"/>
          </propertyfile>
</target>

<target name="testExample">
     <echo message="This target example has the test properties"/>
     <propertyfile
          file="project.properties"
          comment="Setting important system procedure variable to 10 for testing purposes">
          <entry key="importantVariable" value="10"/>
          </propertyfile>
</target>

How it works

The properties file (project.properties) has a key/value pair written to it. Based on what target is run, the key has a different value. This allows us to control how the application works based on how we deploy it. For further reading on Ant's PropertyFile task see their manual.

No comments:

Post a Comment