使用纯Ant来搜索文件列表是否存在并根据条件采取操作

时间:2014-03-14 03:33:50

标签: ant

用户传递XML文件中的文件列表,下面将是示例:

<property-bundle name = "abc">
        <action>clean</action>
        <target-location>/vst/property/pog/</target-location>
        <file-name>test1.props</file-name>
        <file-name>test2.props</file-name>
        <file-name>test3.props</file-name>
</property-bundle>

现在基于该操作删除,我必须在build.xml中合并逻辑以删除目录中的文件,但为此我只想在文件存在时执行验证然后删除或者抛出构建失败错误。我能够从用户输入XML中读取值并将这些文件转换为文件列表属性

  <property name="file.list" value="test1.props,test2.props,test3.props"/>
  <target name = "clean">
     <delete>
           <fileset dir="${target.location}" includes  = "${file.list}"/>
     </delete>
  </target>

但是对于干净的目标,它只验证目录是否存在,因为它是文件集但是如果文件存在则不进行验证,我读到文件列表确实存在文件验证但文件列表可以用于删除。

由于我们在我们的环境中使用Ant 1.6.5我无法使用antcontrib,现在需要大量的流程和批准才能升级Ant。能否指导我如何使用纯Ant实现它。< / p>

5 个答案:

答案 0 :(得分:0)

如果无法删除文件,您应该能够使用delete的{​​{1}}属性,该属性会引发错误。

@failonerror

以上将删除文件,然后在找不到文件时出错,使您处于部分删除状态。如果要避免部分删除,可以先运行另一个任务来检查

<target name = "clean">
  <delete failonerror="true">
     <fileset dir="${target.location}" includes="${file.list}"/>
  </delete>
</target>

尝试复制到临时目录,如果某些目标文件不存在则失败。

答案 1 :(得分:0)

Ant不是一种编程语言,因此没有本机循环机制。在没有外部插件的情况下,可以使用的技巧是XSL转换。将输入XML文件处理为Ant脚本,该脚本在每个文件上实现所需的操作。

实施例

├── build.xml
├── files-process.xsl
├── files.xml          <-- Input listed above
├── test1.props
├── test2.props
└── test3.props

运行构建并删除“files.xml”中列出的文件:

build:
   [delete] Deleting: /home/mark/tmp/test1.props
   [delete] Deleting: /home/mark/tmp/test2.props
   [delete] Deleting: /home/mark/tmp/test3.props

第二次运行构建并生成错误:

BUILD FAILED
/home/mark/tmp/build.xml:6: The following error occurred while executing this line:
/home/mark/tmp/build-tmp.xml:4: file not found: test1.props

的build.xml

使用xslt任务生成包含所需文件删除逻辑的临时Ant脚本:

<project name="demo" default="process-files">

    <target name="process-files">
        <xslt style="files-process.xsl" in="files.xml" out="build-tmp.xml"/>

        <ant antfile="build-tmp.xml"/>
    </target>

    <target name="clean">
        <delete file="build-tmp.xml"/>
    </target>

</project>

文件-process.xsl

以下样式表生成Ant脚本:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/">
    <project name="genbuild" default="build">

      <target name="build">
        <xsl:apply-templates select="property-bundle/file-name"/>
      </target>

    </project>
  </xsl:template>

  <xsl:template match="file-name">
    <available file="{.}" property="{generate-id()}.exists"/>
    <fail message="file not found: {.}" unless="{generate-id()}.exists"/>
    <delete file="{.}" verbose="true"/>
  </xsl:template>

</xsl:stylesheet>

集结tmp.xml

为了提高可读性,我已经格式化了生成的Ant脚本:

<project name="genbuild" default="build">
  <target name="build">

    <available file="test1.props" property="N65547.exists"/>
    <fail message="file not found: test1.props" unless="N65547.exists"/>
    <delete file="test1.props" verbose="true"/>

    <available file="test2.props" property="N65550.exists"/>
    <fail message="file not found: test2.props" unless="N65550.exists"/>
    <delete file="test2.props" verbose="true"/>

    <available file="test3.props" property="N65553.exists"/>
    <fail message="file not found: test3.props" unless="N65553.exists"/>
    <delete file="test3.props" verbose="true"/>
  </target>
</project>

注意:

  • Ant中的属性是不可变的,因此我使用XSL generate-id()函数创建唯一的属性名称。

使用的软件

此示例已使用以下软件版本进行测试:

$ ant -version
Apache Ant version 1.6.5 compiled on June 2 2005

$ java -version
java version "1.7.0_25"
Java(TM) SE Runtime Environment (build 1.7.0_25-b15)
Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)

答案 2 :(得分:0)

可以使用Ant-Contrib Tasks循环遍历文件,然后它将如下所示:

<target name="clean">
  <foreach target="delete.if.exists" param="fileName">
    <fileset dir="${target.location}" includes="${file.list}"/>
  </foreach>
</target>

<target name="delete.if.exists">
   <delete failonerror="true" file="$fileName"/>
</target>

答案 3 :(得分:0)

民间,

谢谢大家的出色帮助&amp;贡献,我终于用下面的

实现了这个目标
  <target name="validate.file" depends="defineAntContribTasks,validate.dir">
    <echo message=" The value of the filelist is ::::::::::::: ${file.list} :::::::::::::::::: "/>
    <for param="file" list="${file.list}">
      <sequential>
         <if>
            <available file="${target.location}/@{file}"/>
            <then>
                <echo message = "File:::  @{file} ::::is valid , Found in :::${target.location}::: "/>
            </then>
            <else>
                  <fail  message=" File:::  @{file} ::::is not valid ,it is not found in :::${target.location}::: ,plesae recheck and submit again"/>
            </else>
         </if>
      </sequential>
    </for>
  </target>

再次感谢您宝贵的时间和指导。

答案 4 :(得分:0)

 <target name="removeUnwantedFiles" description="delete the build destination tree to ensure that it will contain ONLY what is explicitly needed for a build and ONLY what is intended to be release.">
        <delete>
            <fileset dir="${project-home}">
                <includesfile name="${scripts}/excludeJavaFilesForV1.txt"/>
            </fileset>
        </delete>
</target>

这对我有用..希望这会有所帮助..

相关问题