使用ant,重命名目录而不知道完整路径?

时间:2010-04-06 17:48:03

标签: java ant zip rename move

鉴于带有未知目录的zipfile,如何将该目录重命名或移动到规范化路径?

<!-- Going to fetch some stuff -->
<target name="get.remote">

    <!-- Get the zipfile -->
    <get src="http://myhost.com/package.zip"
         dest="package.zip"/>

    <!-- Unzip the file -->
    <unzip src="package.zip"
           dest="./"/>

    <!-- Now there is a package-3d28djh3 directory.  The part after package- is
         a hash and cannot be known ahead of time -->

    <!-- Remove the zipfile -->
    <delete file="package.zip"/>

    <!-- Now we need to rename "package-3d28djh3" to "package".  My best attempt
         is below, but it just moves package-3d28djh3 into package instead of
         renaming the directory. -->

    <!-- Make a new home for the contents. -->
    <mkdir dir="package" />

    <!-- Move the contents -->
    <move todir="package/">
      <fileset dir=".">
        <include name="package-*/*"/>
      </fileset>
    </move>

</target>

我不是一个蚂蚁用户,任何见解都会有所帮助。

非常感谢,-Matt

2 个答案:

答案 0 :(得分:12)

只有dirset只返回1个项目时才会有效。

<project name="Test rename" basedir=".">

  <target name="rename">
    <path id="package_name">
      <dirset dir=".">
        <include name="package-*"/>
      </dirset>
    </path>
    <property name="pkg-name" refid="package_name" />
    <echo message="renaming ${pkg-name} to package" />
    <move file="${pkg-name}" tofile="package" />
  </target>

</project>

答案 1 :(得分:2)

如果package-3d28djh3目录中没有子目录(或者一旦提取它就调用它),你可以使用

<move todir="package" flatten="true" />
  <fileset dir=".">
    <include name="package-*/*"/>
  </fileset>
</move>

否则,使用regexp映射器进行移动任务并删除package-xxx目录:

<move todir="package">
  <fileset dir=".">
    <include name="package-*/*"/>
  </fileset>
  <mapper type="regexp" from="^package-.*/(.*)" to="\1"/>
</move>