gebuh gebuh - 11 months ago 68
Java Question

Apache Ant conditionally copy file from template and load properties

I have a property file (x.props) that contains properties required for an Ant build:



set in build.xml:

<property file="x.props" />

The properties contained in x.props are then used in a path task:

<path id="project.class.path">
<pathelement location="${prop1}/important.jar"/>
<pathelement location="${prop2}/anotherImportant.jar"/>

Before compilation starts. I don't explicitly create these properties in build.xml

The x.props file can be different for each local user, but for server builds it doesn't change.

I created an x.props.template file to address the problem of users accidentally checking in or updating this file (but they need to be able to check it out). I want to be able to create an x.props file from the x.props.template (I set x.props as svn:ignore).

Users can save the template file as x.props and edit it and - theoretically, the server Ant build.xml can copy the template file to the server x.props if it doesn't exist.

But that's not happening. I tried using a target:

<target name="x-props-file" unless="prop1" description="create a file if it doesn't exist">
<copy file="x.props.template" tofile="x.props" />

But it's called AFTER the path is set.

I tried creating the file as a condition:

<condition available="x.props">
<copy file="x.props.template" tofile="x.props" />

But 'not' and 'condition' don't work with copy.

loadProperties will fail if the file isn't found - but it won't copy a file.

Ideally I'd like to be able to copy the new x.props right after the initial file load fails.

Is there any way around this?

ant version 1.1
java 7


Answer Source

This would probably be simplest with ant-contrib's <if> task:

    <available file="x.props" />
    <copy file="x.props.template" tofile="x.props" />