Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ant check existence for a set of files

Tags:

ant

In ant, how can i check if a set of files (comma-separated list of paths) exist or not?

For example I need to check if all paths listed in myprop exist and if so i want to set a property pathExist:

<property name="myprop" value="path1,path2,path3"/>

So in the example all of path1 path2 path3 must exist to set pathExist to true, otherwise false.

I discovered that for a single resource I can use the resourceexist task, but i can't figure out how to use that with a comma-separated list of paths.

How can I check the existence for a set of paths? Thanks!

like image 905
alem0lars Avatar asked Mar 12 '11 09:03

alem0lars


3 Answers

You can use a combination of a filelist, restrict and condition task for this.

In the below example a filelist is created from the property with the comma-separated list of files. Using restrict a list of the files that don't exist is found. This is placed in a property which will be empty if all the files are found.

<property name="myprop" value="path1,path2,path3"/>
<filelist id="my.files" dir="." files="${myprop}" />

<restrict id="missing.files">
  <filelist refid="my.files"/>
  <not>
    <exists/>
  </not>
</restrict>

<property name="missing.files" refid="missing.files" />
<condition property="pathExist" value="true" else="false">
    <length string="${missing.files}" length="0" />
</condition>
<echo message="Files all found: ${pathExist}" />

You could use something like this to generate a failure message listing the missing files:

<fail message="Missing files: ${missing.files}">
    <condition>
        <length string="${missing.files}" when="greater" length="0" />
    </condition>
</fail>
like image 144
martin clayton Avatar answered Oct 13 '22 07:10

martin clayton


Bundled conditions are the shortest solution to check for existence of multiple dirs or files :

<condition property="pathExist">
 <and>
  <available file="/foo/bar" type="dir"/>
  <available file="/foo/baz" type="dir"/>
  <available file="path/to/foobar.txt"/>
  ...
 </and>
</condition>

To check for a commaseparated list of path use Ant addon Flaka , f.e. :

<project xmlns:fl="antlib:it.haefelinger.flaka">

 <!-- when you have a cvs property use split function
      to get your list to iterate over -->
 <property name="checkpath" value="/foo/bar,/foo/baz,/foo/bazz"/>
  <fl:for var="file" in="split('${checkpath}', ',')">
    <fl:fail message="#{file} does not exist !!" test="!file.tofile.exists"/>
  </fl:for>               
</project>

Another possibility is the use of scriptcondition task with a jvm scripting language like groovy,beanshell .. etc.

like image 3
Rebse Avatar answered Oct 13 '22 05:10

Rebse


This can be solved with the set operations of Ant´s resource collections. If you calculate the intersection of the list of required files with the list of existing files it must not differ from the list of required files. This shows how to do it:

<property name="files" value="a.jar,b.jar,c.jar"/>

<target name="missing">
  <condition property="missing">
    <resourcecount when="ne" count="0">
      <difference id="missing">
        <intersect>
          <filelist id="required" dir="." files="${files}"/>
          <fileset id="existing" dir="." includes="*.jar"/>
        </intersect>
        <filelist refid="required"/>
      </difference>
    </resourcecount>
  </condition>
</target>

<target name="check" depends="missing" if="missing">
  <echo message="missing: ${toString:missing}"/>
</target>

The check target reports the list of missing files only if a file is missing.

like image 2
ceving Avatar answered Oct 13 '22 07:10

ceving