Javascript – Ant: how to fail if property contains a certain string

antjavascript

I want to write an ant macro that will call the fail task if the supplied attribute contains a certain string. The only way that I know how to do string comparison in ant is by using javascript. I have something like this:

<macrodef name="check-for-error">
    <attribute name="input"/>
    <sequential>
        <echo message="@{input}"/>
        <script language="javascript">
            <![CDATA[
            var response= "@{input}";
            if(response.indexOf("FAIL") !=-1){
                project.setProperty("error","true");
            }
            ]]>
        </script>
        <fail message="INPUT FAILED" if="${error}"/>
    </sequential>
</macrodef>

The problem with this approach is that I'm setting a property that is global inside of the javascript, and ant does not let you reset a property. I know ant has the ability to set local properties. How can I access local properties from within javascript? Or is there a better way to do this all together?

Best Answer

<condition property="missing-properties">
    <matches pattern="YOUR-PATTERN" string="${THE-ATTRIBUTE}"/>
</condition>
<fail message="Input failed!" if="missing-properties"/>