How to rename files and folders with Ant

2020-04-15 11:20发布

问题:

How to rename many files and folders with Ant? For files I know I can do it like this; question

How to do the same thing for folders?

For e.g. Set of folders (Input)

com.google.appengine.eclipse.sdkbundle_1.5.2.r37v201107211953
com.google.gdt.eclipse.designer.doc.user_2.3.2.r37x201107161328
com.google.gdt.eclipse.designer.hosted.2_0.webkit_win32_2.3.2.r37x201107161253
org.eclipse.acceleo.common_3.1.0.v20110607-0602.jar

Output:

com.google.appengine.eclipse.sdkbundle_1.5.2
com.google.gdt.eclipse.designer.doc.user_2.3.2
com.google.gdt.eclipse.designer.hosted.2_0.webkit_win32_2.3.2
org.eclipse.acceleo.common_3.1.0.jar

回答1:

For complex operations I use the groovy ANT task.

The following example will rename your files and directories, using regular expressions:

<project name="demo" default="rename">

    <target name="bootstrap">
        <mkdir dir="${user.home}/.ant/lib"/>
        <get dest="${user.home}/.ant/lib/groovy-all.jar" src="http://search.maven.org/remotecontent?filepath=org/codehaus/groovy/groovy-all/2.0.6/groovy-all-2.0.6.jar"/>
    </target>

    <target name="rename">
        <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy"/>

        <fileset id="filesToBeRenamed" dir="build"/>
        <dirset  id="dirsToBeRenamed" dir="build"/>

        <groovy>
            project.references.filesToBeRenamed.each { 
                String origName = it
                String newName = origName.replaceAll(/_[0-9\.]+[a-z0-9\-]+/, "")

                if (origName != newName) {
                    ant.move(file:origName, tofile:newName, verbose:"true")
                }
            }

            project.references.dirsToBeRenamed.each { 
                String origName = it
                String newName = origName.replaceAll(/_[0-9\.]+[a-z0-9\-]+/, "")

                if (origName != newName) {
                    ant.move(file:origName, tofile:newName, verbose:"true")
                }
            }
        </groovy>
    </target>
</project>

NOTES:

  • The "bootstrap" target only needs to be run once. It will download the groovy jar from Maven central


回答2:

This will move the directory and all of its sub-directories and files.

  <move todir="${toDir}">
     <fileset dir="${fromDir}"/>
  </move>


回答3:

Take a look at the Ant-Contrib tasks. One is the <for> task. This will allow you to specify multiple directories, directory patterns, etc. This way, you can loop through the directories and files.

You can copy the files and directories to another location and use the Mapper to map the file names. (<move> task will also work with mappers.)

I recommend you download the Ant-Contrib Jar file to the directory ${basedir}/antlib/ac in your project. Then do this in the beginning of your build file:

 <taskdef resource="net/sf/antcontrib/antlib.xml">
    <classpath>
        <fileset dir="${basedir}/antilb/ac"/>
    </classdef>
  <taskdef>

This will define the ant-contrib tasks and allow you to use them. If you're using a version control system and check everything in, someone can checkout your project and do a build without having to install Ant-Contrib first.



标签: ant