0

I have a property with a list of jars delimited with semicolons. The contents of the property is read from a file and not part of the build file, but it looks like this:

<property name="jars" value="a.jar;b.jar;c.jar"/>

And I would like to check if all the files are available. I know how to do it manually using:

<target name="opt">
  <echo message="jars: ${jars}"/>
  <condition property="found">
    <and>
      <available file="a.jar"/>
      <available file="b.jar"/>
      <available file="c.jar"/>
    </and>
  </condition>
  <echo message="found: ${found}"/>
</target>

But how can this be done if the list of files is in a property and can not be written into the build file?

I need something like "apply and map available files". How can this be done?

thekbb
  • 7,668
  • 1
  • 36
  • 61
ceving
  • 21,900
  • 13
  • 104
  • 178
  • 1
    See [Ant check existence for a set of files](http://stackoverflow.com/a/5288804/1078068) for an example. Applying the answer in that question will be easier if `jars` is a comma-separated list. Otherwise, you can convert `jars` from a semi-colon-separated list by using `` as seen in [Replacing characters in Ant property](http://stackoverflow.com/a/14135833/1078068) – Chad Nouis Nov 21 '13 at 16:16
  • @ChadNouis Looks promising but when I try it my Ant 1.7.1 says: `restrict doesn't support the nested "not" element`. – ceving Nov 21 '13 at 16:51
  • I was able to duplicate the error message you see with Ant 1.7.1. This error no longer occurs with Ant 1.8.2. If you stick with Ant 1.7.1, add `xmlns:rsel="antlib:org.apache.tools.ant.types.resources.selectors"` to the `` element. Then change the `` to `` and `` to ``. – Chad Nouis Nov 21 '13 at 17:28
  • Thanks but I got it just by the use of the set operations of the resources. The difference between the required files and the intersection of the required files and the existing files must be zero. That works with 1.7.1 and does not require additional name spaces. You hint about resource collections was very useful! Thanks! – ceving Nov 21 '13 at 17:36

1 Answers1

2

You can use pathconvert to convert your file string to a fileset-include-pattern which can be used in a fileset; e.g.:

<pathconvert property="includespattern" pathsep=",">
    <path path="a.jar;b.jar;c.jar;nonexisting.jar" />
    <globmapper from="${basedir}/*" to="*" handledirsep="true" />
</pathconvert>

<fileset id="your.fileset" dir="${basedir}" includes="${includespattern}"/>
    <!-- fileset will only contain existing files --> 
<echo>${toString:your.fileset}</echo>
R. Oosterholt
  • 7,720
  • 2
  • 53
  • 77