Search code examples
antrenamemove

Relocate contents of dynamic folder


I have a zip file which has one base folder inside it with other content inside that. I don't always know what that base folder is going to be called until I unzip it.

I'd like to move that base folder, and rename it at the same time, in ant - but can't seem to find out how. I've written code to extract the contents of the zip file to ${local.sdk.dir}/temp/ but from here i can't work out how to rename/move the extracted folder

<move todir="${local.sdk.dir}/${remote.sdk.file.name}">
  <fileset dir="${local.sdk.dir}/temp/<WHAT_DO_I_PUT_HERE?>"></fileset>
</move>

also tried

<move todir="${local.sdk.dir}/${remote.sdk.file.name}" includeEmptyDirs="yes" verbose="true">
  <fileset dir="${local.sdk.dir}/temp/" >
     <include name="**/*" />
  </fileset>
</move>

and played about with this, but closest I can get without ant throwing an error is to copy the contents of the temp dir, not the base folder within temp.


Solution

  • You can do all this in one step - copy from the zip file and rename the files changing the dir name as you copy. The copy task accepts a nested resource collection, so you can use a zipfileset to specify the files to copy directly from the zip file.

    In order to rename the files as they are copied, you can use a mapper, which the copy task also takes as a nested element. In this case, a cutsdirmapper looks like the tool for the job.

    So, if I've understood what you want to do correctly, something like this should work:

    <copy todir="${local.sdk.dir}/${remote.sdk.file.name}">
      <zipfileset src="${your.zip.file}" />
      <cutdirsmapper dirs="1" />
    </copy>
    

    cutdirsmapper is only available in Ant 1.8.2 onward, so if you're using an earlier version, you could try a regexpmapper:

    <regexpmapper from="[^/]*(.*)" to="\1" />