p.mesotten
p.mesotten

Reputation: 1402

Ant copy files to dir while renaming original files

I have a task that needs to copy over files to a certain location. If the files already exist in the destination, these destination files need to be renamed (appended with .bak). If the destination file doesn't exist, the file should just be put into place.

Currently I have this:

<target name="install-jsps">
    <copy todir="target">
        <fileset dir="source"/>
        <globmapper from="*.jsp" to="*.jsp.bak"/>
    </copy>
</target>

This however renames the source files while I want to rename the target files before copying over the source files. I cannot do a rename of the whole target folder because some target files are not in the source fileset.

Preferably I don't want to use an external library like ant-contrib.

Upvotes: 4

Views: 2975

Answers (1)

martin clayton
martin clayton

Reputation: 78115

You can do it in two copy tasks: one to make the backups, one to copy in the new files from the source. The extra bit you need is the present selector in the fileset used to make the backup. The selector allows you to only back up the files that are about to be superceded, i.e. the ones that are present in the source directory.

<copy todir="dest">
    <fileset dir="dest" includes="*.jsp">
        <present targetdir="source" />
    </fileset>
    <globmapper from="*" to="*.bak" />
</copy>

<copy todir="dest">
    <fileset dir="source" includes="*.jsp" />
</copy>

Upvotes: 6

Related Questions