How to use wildcard in Ant's Available command

2019-01-25 05:28发布

问题:

I'm using an Ant build script to collate my Eclipse-based application for distribution.

One step of the build is to check that the correct libraries are present in the build folders. I currently use the Ant command for this. Unfortunately, I have to amend the script each time I switch to a new Eclipse build (since the version numbers will have updated).

I don't need to check the version numbers, I just need to check that the file's there.

So, how do I check for:

org.eclipse.rcp_3.5.0.*

instead of:

org.eclipse.rcp_3.5.0.v20090519-9SA0FwxFv6x089WEf-TWh11

using Ant?

cheers, Ian

回答1:

You mean, something like (based on the pathconvert task, after this idea):

<target name="checkEclipseRcp">
  <pathconvert property="foundRcp" setonempty="false" pathsep=" ">
    <path>
      <fileset dir="/folder/folder/eclipse"
               includes="org.eclipse.rcp_3.5.0.*" />
    </path>
  </pathconvert>
</target>

<target name="process" depends="checkEclipseRcp" if="foundRcp">
  <!-- do something -->
</target>


回答2:

A slightly shorter and more straightforward approach with resourcecount condition:

<target name="checkEclipseRcp">
    <condition property="foundRcp">
        <resourcecount when="greater" count="0">
            <fileset file="/folder/folder/eclipse/org.eclipse.rcp_3.5.0.*"/>
        </resourcecount>
    </condition>
</target>

<target name="process" depends="checkEclipseRcp" if="foundRcp">
  <!-- do something -->
</target>


回答3:

The pathconvert task is probably the preferred way to go in most cases. But it creates a little problem when the directory tree is very large and one uses the echoproperties task. With a very large directory tree, the string generated by pathconvert can be huge. Then echoproperties sprays the huge string, making the output more difficult to work with. I use a macrodef on Linux that creates a property set to "1" if there are files in the directory:

<macrodef name="chkDirContents" >
    <attribute name="propertyName" />
    <attribute name="dirPath" />
    <attribute name="propertyFile" />
    <sequential>
        <exec executable="sh" dir="." failonerror="false" >
            <arg value="-c" />
            <arg value='fyles=`ls -1 @{dirPath} | head -1` ; if [ "$fyles" != "" ] ; then echo @{propertyName}=1 > @{propertyFile} ; fi' />
        </exec>
    </sequential>
</macrodef>

<target name="test" >
    <tempfile destdir="." property="temp.file" deleteonexit="true" />
    <chkDirContents propertyName="files.exist" dirPath="./target_dir" propertyFile="${temp.file}" />
    <property file="${temp.file}" />

    <echoproperties/>
</target>

Executing the "test" target will generate the following echoproperties line if there are files in the ./target_dir/ directory:

[echoproperties] files.exist=1

What "test" does: It generates a temporary filename, ${temp.file}, that can later be used as a property file. It then executes the macrodef, which calls the shell to check the contents of the dirPath directory. If there are any files or directories in dirPath, it assigns the propertyName property a value of 1 in the temporary file. It then reads the file and sets the property given in the file. If the file is empty, no property is defined.

Note that the temporary file could be reused for subsequent calls of the macrodef if desired. On the other hand, of course, once a property is set, it is immutable.



标签: java ant