30

How to rename file while copying it to directory in ant?

<copy file="..." todir="..." overwrite="true">
martin clayton
  • 76,436
  • 32
  • 213
  • 198
Martin
  • 767
  • 2
  • 9
  • 21
  • 1
    The copy task can take a nested [file mapper](http://ant.apache.org/manual/Types/mapper.html) to do renaming. – matt Jan 23 '12 at 15:57

3 Answers3

52

Use tofile option instead of todir


Added

Or a more complex example from Ant Copy Task documentation:

Copy a set of files to a directory, appending .bak to the file name on the fly

  <copy todir="../backup/dir">
    <fileset dir="src_dir"/>
    <globmapper from="*" to="*.bak"/>
 </copy>
helios
  • 13,574
  • 2
  • 45
  • 55
  • 9
    Perfect. Good old Ant. Been a while since I've really used Ant much, and recently it has been relegated to performing small tasks as an adjunct to Maven. With the new kids in town like Grunt etc, Ant has taken a fair share of bashing. But you know what, like the simplicity of the above solutions demonstrates, Ant is still a venerable old tool and, XML or no XML, should be given more respect. – arcseldon Sep 24 '13 at 05:07
10

It should be as simple as

<copy file="mySourceFile" tofile="MyDestFile" />
Peter Szanto
  • 7,568
  • 2
  • 51
  • 53
  • Your suggestion brought me an error: `An Ant BuildException has occured: Only one of tofile and todir may be set`. Did not find how to fix it but found other solution for copying a single file: `` – user435421 May 03 '18 at 13:20
0

something like this also works, in case you want to specify a directory within the tofile attribute:

<target name="-post-jar" depends="init,compile">
    <copy file="${basedir}/src/query.txt" overwrite="true" tofile="${dist.dir}/input.txt" />
</target>
hello_earth
  • 1,442
  • 1
  • 25
  • 39