Checking a reference with ant
Is there any way to check in ant that a reference with specific refid is defined somewhere?
For example, I need to check that my.ref is defined before using it as follows.
<pathconvert property="my.prop" refid="my.ref">
...
</pathconvert>
I need to do it since in my project there are some build scripts which not under my control (actually, I am not allowed to examine their content sometimes).
These scripts are supposed to define this reference. Unfortunately, I can't count that this reference is defined due to some reason.
So, I need to check it and perform graceful error handling or some specific actions.
Is there any way to do it?
Update:
I found the correct way to do it myself.
It can be done by using isreference
elem开发者_如何学运维ent of the condition
task.
For instance:
<condition property="my.ref.defined">
<isreference refid="my.ref"/>
</condition>
<fail unless="my.ref.defined" message="Reference my.ref not defined."/>
You can set a property based on the value of a reference, then test that property. For example:
<property name="my.prop" value="${toString:my.ref}" />
To make sure it works with all types of references it's better to use ${ant.refid:reference}, see Ant Manual, though it works with ${toString:reference} in most cases.
With Ant Plugin Flaka it's a one-liner =
<project name="demo" xmlns:fl="antlib:it.haefelinger.flaka">
<fl:fail message="Houston we have a problem" test="not has.property['ant.refid:my.ref']"/>
Flaka has also control structures like when, unless, while, for, choose, switch ..
<fl:when test="not has.property['ant.refid:my.ref']">
...
</fl:when>
see Flaka Manual
精彩评论