ANT:检查文件是否为空

ANT: check if file is empty

本文关键字:是否 文件 检查 ANT      更新时间:2023-09-26

我正在尝试在处理文件之前检查文件是否为空,所有这些都来自我的 ANT 任务。

基本上,我有以下目标由

另一个主要目标调用,我有其他目标在此目标之后调用,我想运行此目标发生的任何情况:

 <target name="mytarget">
<!-- Copy a file -->
      <get verbose="true" ignoreerrors="no"
          src="..."
          dest="bla.txt" />
      <property name="file.bla" value="bla.txt" />
<!-- Check if the file is empty -->
      <script language="javascript"> <![CDATA[
         importClass(java.io.File);
         file = project.getProperty("file.bla");
         var filedesc = new File(file);
         var size = filedesc.size;
         if (size==0){
            //? Exit the target cleanly
         }
       ]]> </script>
<!-- If the file is not empty, process it with ANT. -->
<!-- ... ->
</target>
  • 在没有 ANT Contrib 的情况下检查文件大小的最简单方法是什么?
  • 如何在不破坏其余目标的情况下退出当前目标执行?

所以我设法做了我想做的事情,这是检查文件大小并根据大小运行其他内容的基本示例。

创建仅运行您的条件的目标

<target name="check.log.file">
   <get src="bla/problems.txt" dest="${temp.dir}'bla.txt" />
   <property name="file" value="${temp.dir}'bla.txt" />
   <condition property="file.is.empty">
     <length file="${file}" when="equal" length="0" />
   </condition>
   <antcall target="target.to.process.the.log.file"/>
</target>

编辑处理目标

现在,在另一个目标中,取决于您的条件的目标只需添加:

<target name="target.to.process.the.log.file" unless="file.is.empty">
<!-- whatever you do -->
</target>

这就是所有人。