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?
<condition property="missing-properties">
<matches pattern="YOUR-PATTERN" string="${THE-ATTRIBUTE}"/>
</condition>
<fail message="Input failed!" if="missing-properties"/>
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( );
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With