3
votes

I'm trying to check if a file is empty before processing it, all of that from my ANT task.

Basically I have the following target that is called by another main target, i have other targets called after this one that I want to run whatever happens to this one:

 <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>
  • What is the easiest way to check a file size without ANT Contrib?
  • How do I quit the current target without breaking the rest of the execution?
1
Thanks, very useful, I didn't find it before :) - Flag

1 Answers

2
votes

So I managed to do what I wanted, here's the basic example to check file sizes and run other things depending on the size.

Create a target that only runs your condition

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

Edit the processing target

And now, in the other target, the one that depends on your condition just add:

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

That's all folks.