Renaming files during ANT copy
Asked Answered
F

3

14

I'd like to copy a directory of files, and rename some of them in the process. When I run the script below, nothing copies. If I comment the glob mappers in the file below, the files are copied (without the renaming)

Thanks for any help. James

<?xml version="1.0" ?>
<project name="Create project structure" default="main">
  <target name="main" description="Copy template files to project folder">
    <echo>Copying template files to project folder</echo>
    <copy todir="${project.dir}" verbose="true" overwrite="true">
      <fileset dir="${shared.files}/templateproject" excludes=".svn"/>
      <mapper>
        <chainedmapper>
          <mapper type="glob" from="*PACKAGENAME*" to="*${package.name}*"/>
          <mapper type="glob" from="*GAMENAME*" to="*${game.name}*"/>
          <mapper type="identity"/>
        </chainedmapper>
      </mapper>
    </copy>
  </target>
</project>
Fermat answered 7/8, 2009 at 10:34 Comment(0)
F
14

Resorted to a workaround, using "move", and the correct mapper type as indicated by Mnementh. Thanks

<?xml version="1.0" ?>
<project name="Create project structure" default="main">
    <target name="main" description="Copy template files to project folder">
    <echo>Copying template files to project folder</echo>
    <copy todir="${project.dir}" verbose="true" overwrite="true">
        <fileset dir="${shared.files}/templateproject" excludes=".svn" />
    </copy>
    <move todir="${project.dir}">
        <fileset dir="${project.dir}" />
        <mapper>
        <mapper type="regexp"
                from="(.*)PACKAGENAME(.*)" to="\1${package.name}\2" />
        <mapper type="regexp"
                from="(.*)GAMENAME(.*)" to="\1${game.name}\2" />
        </mapper>
    </move>
    </target>
</project>
Fermat answered 7/8, 2009 at 12:28 Comment(1)
Good solution! One thing that doesn't seem to work is if you have multiple items to replace. E.g. consider the path: PACKAGENAME\blah\foo\PACKAGENAME. What I observed was that only the first rename works. My crude workaround was to simply use a <for> loop to do this move 10 times. Ugly but it seems to work.Latrice
T
11

It seems, that the glob-mapper works only with one '*'. I would try the regexp-mapper:

<mapper type="regexp" from="(.*)PACKAGENAME(.*)" to="\1${package.name}\2"/>
<mapper type="regexp" from="(.*)GAMENAME(.*)" to="\1${game.name}\2"/>
Terence answered 7/8, 2009 at 10:42 Comment(1)
Thanks for your answer. That is the right type of mapper to use. However, the mappers only want to work one at a time - if I comment out two of them, the other one will work. I need them to each of them to be tested - try the first replace, then the second replace, if not copy anyway (the identity mapper). Thanks anyway - this has been helpful.Fermat
P
6

Your problem is that you did not choose the right mapper: <chainedmapper> will pass information in chain from the first to the last mapper

Instead, <firstmatchmapper> should be used, which will try all mappers in turn, until one matches

Reference: http://ant.apache.org/manual/Types/mapper.html

(quite an old question, but I just found searching for almost the same problem :-))

Papilloma answered 2/9, 2011 at 8:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.