ANT删除空目录或使用index.html文件


ANT delete empty directories or with index.html file

我正在用Ant构建一个免费的专业包
在专业版中,没有一些文件,因为我可以使用自定义PhpDoc标记跳过它们。

遗憾的是,使用这个系统,我有一些只有index.html的空目录,我想删除它们,这样我就可以分发一个"干净"的包。

我需要删除的目录遵循以下路径:

views
  `- {directoryName1}
      `- tmpl
         `- index.html
  `- {directoryName2}
   .... and son on ....

只有directoryName每次都会更改,但一切都保持不变
我尝试过这个蚂蚁任务,但没有成功:

<delete includeemptydirs="true">
    <fileset dir="${buildDir}/free">
       <exclude name="**/*" />
       <include name="**/tmpl/index.html"/>
    </fileset>
</delete>

有什么帮助吗?

您需要资源计数和条件检查的组合。我用蚂蚁悔过剂,但你必须包括额外的罐子。

<project name="blah" basedir="." default="conditionally.clean">
    <taskdef resource="net/sf/antcontrib/antlib.xml"/>
    <macrodef name="m.clean">
        <attribute name="dir.todelete"/>
        <sequential>
            <local name="count.files"/>
            <resourcecount property="count.files">
                <fileset dir="@{dir.todelete}">
                    <exclude name="index.html"/>
                </fileset>
            </resourcecount>
            <if>
                <equals arg1="${count.files}" arg2="0"/>
                <then>
                    <echo message="will delete dir @{dir.todelete}"  />
                </then>
                <else>
                       <echo message="will not delete dir @{dir.todelete}"  />
                </else>
            </if>
        </sequential>
    </macrodef>
    <target name="conditionally.clean">
        <m.clean dir.todelete="etc/demo2"/>
        <m.clean dir.todelete="etc/demo1"/>
    </target>
</project>

您可以构造一个"foreach"(需要ant contrib)来获取所有目录并调用上面的宏。。。

(我想自定义任务或嵌入式脚本可能比build.xml干净得多…)