Ant: as a failure if the property contains a specific string

I want to write an ant macro that will fail if the incoming attribute contains a specific string. The only way I know how to do string comparisons in ant is to use 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 am setting a property that is global inside javascript, and ant does not allow you to reset the property. I know that ant has the ability to set local properties. How can I access local properties from javascript? Or is there a better way to do this all together?

+8
javascript ant
source share
2 answers
 <condition property="missing-properties"> <matches pattern="YOUR-PATTERN" string="${THE-ATTRIBUTE}"/> </condition> <fail message="Input failed!" if="missing-properties"/> 
+8
source share

All you have to do to localize the property is to call the local task for it before Javascript.

For example:

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

Alternatively, you can localize the enitrely property in Javascript:

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

All Articles