tags:

views:

165

answers:

1

Ant replace task does a in-place replacement without creating a new file.

The below snippet replaces tokens in any of the *.xml files with the corresponding values from my.properties file.

<replace dir="${projects.prj.dir}/config" replacefilterfile="${projects.prj.dir}/my.properties" includes="*.xml" summary="true"/'>

I want those *.xml files that had their tokens replaced be created as *.xml.filtered (for e.g.) and still have the original *.xml.

Is this possible in Ant with some smart combination of tasks and concepts ?

A: 

One approach might be to copy all the xml files to a work directory, carry out the replace there, then copy the files back to the source directory with a mapper that renames the files on the way.

Example:

<delete dir="work" />
<mkdir dir="work" />

<copy todir="work">
  <fileset dir="${projects.prj.dir}">
    <include name="*.xml" />
  </fileset>
</copy>

<replace dir="work" replacefilterfile="my.properties" includes="*.xml" summary="true" />

<copy todir="${projects.prj.dir}">
  <fileset dir="work">
    <include name="*.xml" />
  </fileset>
  <mapper type="glob" from="*.xml" to="*.xml.filtered"/>
</copy>
martin clayton