adding comment in .properties files

Ashwin Hegde picture Ashwin Hegde · Apr 10, 2013 · Viewed 102.4k times · Source

By using following block of code in build.xml file

<propertyfile file="default.properties" comment="Default properties">
   <entry key="source.dir" value="1" />
   <entry key="dir.publish" value="1" />
   <entry key="dir.publish.html" value="1" />
</propertyfile>

I am able to generate default.properties file with following file contents

source.dir=1
dir.publish=1
dir.publish.html=1

I want to know how can I add my comments in the generated file? E.g. the generated properties should have the following content:

# Default Configuration
source.dir=1
dir.publish=1
# Source Configuration
dir.publish.html=1

How can I do it dynamically using Ant's build.xml?

Answer

David W. picture David W. · Apr 10, 2013

The property file task is for editing properties files. It contains all sorts of nice features that allow you to modify entries. For example:

<propertyfile file="build.properties">
    <entry key="build_number"
        type="int"
        operation="+"
        value="1"/>
</propertyfile>

I've incremented my build_number by one. I have no idea what the value was, but it's now one greater than what it was before.

  • Use the <echo> task to build a property file instead of <propertyfile>. You can easily layout the content and then use <propertyfile> to edit that content later on.

Example:

<echo file="build.properties">
# Default Configuration
source.dir=1
dir.publish=1
# Source Configuration
dir.publish.html=1
</echo>
  • Create separate properties files for each section. You're allowed a comment header for each type. Then, use to batch them together into one single file:

Example:

<propertyfile file="default.properties"
    comment="Default Configuration">
    <entry key="source.dir" value="1"/>
    <entry key="dir.publish" value="1"/>
<propertyfile>

<propertyfile file="source.properties"
    comment="Source Configuration">
    <entry key="dir.publish.html" value="1"/>
<propertyfile>
<concat destfile="build.properties">
    <fileset dir="${basedir}">
        <include name="default.properties"/>
        <include name="source.properties"/>
    </fileset>
</concat>

<delete>
    <fileset dir="${basedir}">
         <include name="default.properties"/>
        <include name="source.properties"/>
    </fileset>
</delete>