删除通过zipfileset指定的文件?

时间:2013-02-13 04:05:03

标签: ant

我有以下Ant目标,它从特定的.ZIP存档中提取内容:

<!-- UNPACK-MATH -->
<target name="unpack-math" depends="init-contrib">
  <!-- NOTE: the 'unzip' task doesn't fail when it cannot extract over read-only files; however, 'copy' with a 'zipfileset' does. -->
  <first id="math.archive">
    <fileset dir="${builddir}" includes="MATH_MF*.zip" />
  </first>
  <if>
    <length string="${toString:math.archive}" when="greater" length="0" />
    <then>
      <copy todir="${basedir}">
        <zipfileset src="${toString:math.archive}" />
      </copy>
    </then>
    <else>
      <echo message="No math to unpack." />
    </else>
  </if>
</target>

我现在要做的是“清理”提取的文件。但是,以下内容不起作用:

<!-- CLEAN-MATH -->
<target name="clean-math" depends="init-contrib">
  <first id="math.archive">
    <fileset dir="${builddir}" includes="MATH_MF*.zip" />
  </first>
  <if>
    <length string="${toString:math.archive}" when="greater" length="0" />
    <then>
      <delete>
        <zipfileset src="${toString:math.archive}" />
      </delete>
    </then>
    <else>
      <echo message="No math to clean." />
    </else>
  </if>
</target>

我得到以下堆栈跟踪:

BUILD FAILED
D:\Development\MForce\Games\gamebuild.xml:214: java.lang.ClassCastException: class org.apache.tools.ant.types.resources.ZipResource doesn't provide files
        at org.apache.tools.ant.types.resources.comparators.FileSystem.resourceCompare(FileSystem.java:43)
...

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

此解决方案似乎有效,但需要首先解压缩.ZIP存档(其中列出了您想要删除的文件作为其他根目录),我宁愿避免这样做:

<!-- CLEAN-MATH -->
<target name="clean-math" depends="init-contrib">
  <first id="math.archive">
    <fileset dir="${builddir}" includes="MATH_MF*.zip" />
  </first>
  <if>
    <length string="${toString:math.archive}" when="greater" length="0" />
    <then>
      <unzip dest="${builddir}/tmp" src="${toString:math.archive}"/>
       <delete>
        <fileset dir="${basedir}" includes="**/*">
          <present present="both" targetdir="${builddir}/tmp"/>
        </fileset>
      </delete>
      <delete dir="${builddir}/tmp"/>
    </then>
    <else>
      <echo message="No math to clean." />
    </else>
  </if>
</target>