gebuh gebuh - 1 month ago 12
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:

x.props:

prop1=something
prop2=somethingElse


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"/>
</path>


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
local.properties file if it doesn't exist">
<copy file="x.props.template" tofile="x.props" />
</target>


But it's called AFTER the path is set.

I tried creating the file as a condition:

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


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

Thanks

Answer

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

<if>
  <not>
    <available file="x.props" />
  </not>
  <then>
    <copy file="x.props.template" tofile="x.props" />
  </then>
</if>
Comments