使用纯 Ant 搜索文件列表是否存在并根据条件采取行动 [英] Use pure Ant to search if list of files exists and take action based on condition

查看:21
本文介绍了使用纯 Ant 搜索文件列表是否存在并根据条件采取行动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户在一个 XML 文件中传递文件列表,下面是示例:

User passes a list of files in an XML file, below will be the sample:

<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 中读取值并将这些文件放入文件列表属性中

Now based on that action remove, I have to incorporate logic in build.xml to delete the files in the directory , but for that I want to perform a validation only if the file exists then remove or else throw the build failure error. I was able to read the values from the user input XML and takes those files into a file list property

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

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

but with the clean target it only validates if the directory exists since it is fileset but does not do the validation if file exists , I read that filelist does validation for file exists but filelist can work with delete.

由于我们在我们的环境中使用 Ant 1.6.5,我不能使用 antcontrib,现在升级 Ant 需要很多过程和批准,请您指导我如何使用纯 Ant 实现它.

Since we are using Ant 1.6.5 in our environment I can not use antcontrib , It takes whole lot of process and approvals to upgrade Ant now , Can you please guide me on how it can be achieved with the pure Ant.

推荐答案

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

Ant is not a programming language and therefore has no native looping mechanism. In the absence of external plugins a trick that can used is an XSL transformation. Process the input XML file into a Ant script which implements the desired operation on each file.

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

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

Run the build and the files listed in "files.xml" are deleted:

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

再次运行构建并产生错误:

Run the build a second time and an error is generated:

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 脚本:

build.xml

Use the xslt task to generate a temporary Ant script containing the desired file deletion logic:

<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>

files-process.xsl

以下样式表生成一个 Ant 脚本:

files-process.xsl

The following stylesheet generates an Ant script:

<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>

build-tmp.xml

为了提高可读性,我对生成的 Ant 脚本进行了格式化:

build-tmp.xml

To aid readability I have formatted the generated Ant script:

<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() 函数来创建唯一的属性名称.

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

This example was tested with the following software versions:

$ 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)

这篇关于使用纯 Ant 搜索文件列表是否存在并根据条件采取行动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆