Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ant: how to fail if property contains a certain string

Tags:

javascript

ant

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?

like image 281
Kyle Avatar asked Jan 25 '11 22:01

Kyle


2 Answers

<condition property="missing-properties">
    <matches pattern="YOUR-PATTERN" string="${THE-ATTRIBUTE}"/>
</condition>
<fail message="Input failed!" if="missing-properties"/>
like image 163
Entendu Avatar answered Nov 15 '22 15:11

Entendu


All that you need to do to localise the property is to call the local task for it prior to the Javascript.

For example:

<sequential>
    <echo message="@{input}"/>
    <local name="error"/>        <!-- Added this line. -->
    <script language="javascript">
    ...

Also, instead, you might localise the property enitrely in Javascript:

<script language="javascript"><![CDATA[
    localiser = project.createTask( "local" );
    localiser.setName( "error" );
    localiser.perform( );

    ...
like image 21
martin clayton Avatar answered Nov 15 '22 15:11

martin clayton