【问题标题】:Check multiple file exists or not using ANT使用 ANT 检查多个文件是否存在
【发布时间】:2013-03-29 06:45:29
【问题描述】:

我正在使用 ANT 来检查两个 jar 中的一组文件的计数。 我无法检查是否存在相同模式的文件。

例如我有 2 个文件,例如 /host/user/dir1/ABC1.txt 和 /host/user/dir1/ABC2.txt。

现在我想检查模式“/host/user/dir1/ABC*”的文件是否存在??

我可以使用可用标签检查单个文件 /host/user/dir1/ABC1.txt,但无法检查文件的特定模式。

提前致谢。

对于单个文件,以下工作正常:

<if>
    <available file="${client.classes.src.dir}/${class_to_be_search}.class"/>
    <then>
         <echo> File ${client.classes.src.dir}/${class_to_be_search}.class FOUND in src dir 

         </echo>
         <echo> Update property client.jar.packages.listOfInnerClass</echo>
    </then>
    <else>
         <echo> File ${client.classes.src.dir}/${class_to_be_search}.class NOT FOUND      in src dir.  
         </echo>
    </else>
</if>

但我想搜索多个文件: 类似于:${dir.structure}/${class_to_be_search}$*.class

【问题讨论】:

  • 你能添加你实际使用的代码吗(这个带有单独的文件检查)?
  • 对于单个文件,以下工作正常: 文件 ${dir.structure}/${class_to_be_search}.class 在 src 目录中找到。 更新属性 client.jar.packages.listOfInnerClass 文件 ${client.classes.src.dir}/${class_to_be_search}.class NOT FOUND在 src 目录中。 我要搜索 ${dir.structure}/${class_to_be_search}$*.class
  • 编辑您的问题并将其粘贴到那里。不要忘记将其格式化为可读形式。
  • @Patryk Roszczyniała:用示例编辑的问题。

标签: ant ant-contrib


【解决方案1】:

if task 不是核心 ANT 的一部分。

以下示例显示了如何使用 ANT condition task 完成此操作。您可以在文件集中使用您想要的任何模式。目标执行随后取决于“file.found”属性的设置方式:

<project name="demo" default="run">

    <fileset id="classfiles" dir="build" includes="**/*.class"/>

    <condition property="file.found">
        <resourcecount refid="classfiles" when="greater" count="0"/>
    </condition>

    <target name="run" depends="found,notfound"/>

    <target name="found" if="file.found">
        <echo message="file found"/>
    </target>

    <target name="notfound" unless="file.found">
        <echo message="file not found"/>
    </target>

</project>

【讨论】:

  • 您已将 target="run" 设为默认值。这意味着它将默认首先运行。正确的?这个“运行”目标取决于找到/未找到的目标,而这些目标又使用“file.found”条件。我想知道这个条件任务何时执行并设置 file.found=true/false ??
  • @MayurSharma 这就是“条件”任务的目的。它在构建初始化并设置属性时执行。该属性反过来控制“found”和“notfound”目标是否执行任何操作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-19
  • 2022-11-04
  • 1970-01-01
  • 1970-01-01
  • 2022-01-08
  • 1970-01-01
相关资源
最近更新 更多