0

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

gebuh
  • 797
  • 14
  • 40

1 Answers1

2

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>

Edit: I just rediscovered this answer that I gave years ago after a comment was added, and realized that there's a much better solution that doesn't rely on ant-contrib. It's probably way too late to be of much use to the original poster, but I figured I'd offer it up anyway in case it helps someone:

<property file="x.props" />

<property name="prop1" value="something" />
<property name="prop1" value="somethingElse" />

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

No need for an extra template file. Just attempt to load the property file first and set your defaults immediately after that, making use of Ant's inherent property immutability.

Conversely, if you really want to use a separate properties file to hold your default values, just do the same thing as above but replace the individual property assignments with another property file load:

<property file="x.props" />
<property file="default.props" />
CAustin
  • 4,525
  • 13
  • 25
  • doh, I was trying to avoid the use of ant-contrib, had I known it would solve the problem so handily I could have saved myself some time. I used: http://stackoverflow.com/questions/12891893/how-to-include-ant-contrib-jar-dynamically-in-ant to add it. Thanks – gebuh Apr 04 '14 at 04:06
  • 1
    The element not must be terminated with ` ` – nelsonomuto Oct 21 '16 at 18:25