Setting Properties Outside of the Buildfile

Q: We’ve tried to make a universal buildfile, but have run into issues where certain property values will differ on each developer’s workstation. We don’t want developers to edit the buildfile. How do we do this?

Ant’s property task has an attribute that takes a filename. If the file is structured as a properties file (name-value pairs, one per line), Ant includes these values in the property table. If a name conflicts with a name specified in the buildfile, and the property file is included before the in-buildfile definition (this part’s very important), the value overrides the buildfile’s value. The key is that the first definition of a property takes precedence.

By declaring an external property file for use in the build, you give developers a way to override buildfile property values without editing the buildfile. The following buildfile snippet shows an example of how the buildfile includes an external properties file in case the codebase property needs to be overridden:

<project name="ExtenisbleProject" default="all" basedir=".">
    <property name="build.properties"/>
    <property name="codebase" value="/export/home/builduser"/>
...the rest of the properties and buildfile
...

This buildfile imports properties from build.properties before setting the properties internally. If a developer specifies a value for codebase in the build.properties file, that developer-specific setting takes precedence because it was defined first. Otherwise, if ...

Get Ant: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.