Using ant, check if a particular string is found in a file

ant

Solution

Here's an example of one way you might do this:

<target name="wait-for">
    <waitfor maxwait="15" maxwaitunit="second" timeoutproperty="build.timeout">
        <resourcecontains resource="build.log" substring="Build Successful" />
    </waitfor>
    <antcall target="build-success" />
</target>

<target name="build-success" depends="build-fail" unless="build.timeout">
    <echo message="Success" />
</target>
<target name="build-fail" if="build.timeout">
    <echo message="Fail" />
</target>

Use the `resourcecontains` condition to look for the string in the named resource - in this case the file 'build.log'. If it's not found in the allotted time, the `build.timeout` property is set. There are two targets, one that is to be run if the string is found, the other if not. The 'target' attributes `if`, `unless`, and `depends` are used to make the if-else logic need. If you only need to take an action in the case of success or failure, you can simplify slightly.

Problem

My requirement is that, using waitfor condition, ant should periodically check if string "Build Successful" is displayed in log file. If the string is found, then particular action should be performed.

Original source