Ant condition block - ant

I have an ant condition like this:
<condition property="create_stub">
<and>
<available file="${create_stub_command_file}" property="stub_script.present" />
<isset property="packaged_stub_file"/>
</and>
</condition>
My understanding is: If create_stub_command_file is present then set stub_script.present=true. But I am not sure about
<isset property="packaged_stub_file"/>
What is this doing? And how does it change the overall condition. i.e In which case would the condition block evaluate to true?

A slight mistake?
<condition property="create_stub">
<and>
<available file="${create_stub_command_file}" property="stub_script.present" />
<isset property="packaged_stub_file"/>
</and>
</condition>
I don't believe that property="stub_script.present" is doing anything. It should be:
<condition property="create_stub">
<and>
<available file="${create_stub_command_file}"/>
<isset property="packaged_stub_file"/>
</and>
</condition>
All that condition statement is doing is setting a property called create_stub. It will set the property if both a file or directory called whatever {$create_stub_command_file} exists, and if the property packaged_stuf_file is set to any value. The property packaged_stub_file can be set to false, to a null string, to true, to YES! YES! YES! or to anything, as long as it is set.
So now you can use this property as a test for a target:
<target name="package_stub"
if="create_stub">
<blah...blah...blah/>
<yadda...yadda...yadda/>
</target>
This target, package_stub will only execute if the property package_stub is set. And it will only be set if that <condition> above is true.
That <condition> statement should be outside of any targets, so it will be executed first before any targets are executed.

Equivolent pseudo code:
if (File($create_stub_command_file).exists) then
property["stub_script.present"] := true
end
if (File($create_stub_command_file).exists AND property["property["stub_script.present"] != NULL) then
property["create_stub"] := true
end
Forgive any errors... I find condition blocks tricky and they need lots of testing. You're best advised to keep them simple. ANT is not a programming language.

Related

ant how to compare a property and a string value

I have a problem with my ant script. basically i want to read a property and compare it with a string.
<property name="server_environment" value="local"/>
...
<condition property="blabla2">
<equals arg1="local" arg2="local" trim="true" forcestring="true"/>
</condition>
<condition property="blabla">
<equals arg1="{$server_environment}" arg2="local" trim="true" forcestring="true"/>
</condition>
Now in the output we get (after turning -debug on)
Setting project property: server_environment -> local
...
Condition true; setting blabla2 to true
Setting project property: blabla2 -> true
Condition false; not setting blabla
I do not understand why blabla evaluates to false here. Perhaps something is going wrong when reading the server_environment property. But i am not sure what.
Apparantly i needed to change to position of the $. I changed
<equals arg1="{$server_environment}" arg2="local" trim="true" forcestring="true"/>
Into
<equals arg1="${server_environment}" arg2="local" trim="true" forcestring="true"/>
Now it does set the property correctly.

Ant isset behaviour

According to the documentation isset clause "Test whether a given property has been set in this project". I don't understand wheter isset returns true or false when a property is set
In the below code snippet java.pass.home is set to somval when env.JAVA_HOME is not set .
<condition property="java.passed.home" value="somval">
<isset property="${env.JAVA_HOME}"/>
</condition>
This snippet works for my requirement but i think the correct code snippet should be something like this as i want to set the property when another property is not available
<condition property="java.passed.home" value="somval">
<not>
<isset property="${env.JAVA_HOME}"/>
</not>
</condition>
Can some one please clarify this?
Thanks in advance
The code snippet was right but the property should be mentioned with just name without enclosing it with {}
<isset property="${env.JAVA_HOME}"/>
It should be
<isset property="env.JAVA_HOME"/>

antcall based on a condition

This is what I am trying to achieve:
if a property is set then call antcall target. is this doable? can someone tell me how?
<condition>
<isset property="some.property">
<antcall target="do.something">
</isset>
</condition>
Something like this should work:
<if>
<isset property="some.property"/>
<then>
<antcall target="do.something"/>
</then>
</if>
If then conditions require ant-contrib, but so does just about anything useful in ant.
I know I'm really late to this but here is another way to do this if you are using an of ant-contrib where if doesn't support a nested antcall element (I am using antcontrib 1.02b which doesn't).
<target name="TaskUnderRightCondition" if="some.property">
...
</target>
You can further expand this to check to see if some.property should be set just before this target is called by using depends becuase depends is executed before the if attribute is evaluated. Thus you could have this:
<target name="TestSomeValue">
<condition property="some.property">
<equals arg1="${someval}" arg2="${someOtherVal}" />
</condition>
</target>
<target name="TaskUnderRightCondition" if="some.property" depends="TestSomeValue">
...
</target>
In this case TestSomeValue is called and, if someval == someOtherVal then some.property is set and finally, TaskUnderRightCondition will be executed. If someval != someOtherVal then TaskUnderRightCondition will be skipped over.
You can learn more about conditions via the documentation.
Consider also you can invoke groovy for these purposes:
<use-groovy/>
<groovy>
if (Boolean.valueOf(properties["some.property"])) {
ant.project.executeTarget("do.something")
}
</groovy>

How to make a property in Ant as mutable

I'm using ant condition task to check a file existence and directory existence and below is my code
<project name="makeitmutable" basedir="." default="main">
<target name="main">
<condition property="folderexists?" value="Yeah" else="Nope">
<and>
<available file="folderexistance" type="dir"/>
<available file="a.zip" type="file"/>
</and>
</condition>
<echo>before deleting "folderexistance" folder property folderexists?=${folderexists?}</echo>
<delete dir="folderexistance"/>
<!--after delete-->
<condition property="folderexists?" value="Yeah" else="Nope">
<and>
<available file="folderexistance" type="dir"/>
<available file="a.zip" type="file"/>
</and>
</condition>
<!--how to make below line to print Nope ?-->
<echo>After deleting "folderexistance" folder property folderexists?=${folderexists?}</echo>
</target>
</project>
My output value of the property folderexists? remains same even after deleting the directory,i.e.., Nope two times
I knew that ant properties are immutable once set cannot be changed,and also an alternative to this solution is we can use
<antcall>
task and call the main target.
Is there a way to make the property mutable within that target as in the above scenario,I'm looking for other possibilities to resolve this, what's the better programming practice for this type of problem.
As you said, properties are immutable. The only other option is to use the var task from ant-contrib.
Quote from the docs: In general, use of this task is DISCOURAGED, and the standard Ant Property should be used if possible. Having said that, in real life I use this a lot.
which says a lot, too ;-)

conditional property setting

How can i set a single property to different values based on conditions. My scenario is as follows:
1) Loop through different values of messageid
2) Give different 'comment' for each messageid
<for list="12,23,34,45" param="messageid">
<sequential>
<condition property="comment" value="wireMsg-Inbound">
<equals arg1="messageid" arg2="12"/>
</condition>
<condition property="comment" value="wireMsg-Outbound">
<equals arg1="messageid" arg2="12"/>
</condition>
<condition property="comment" value="appMsg-Inbound">
<equals arg1="messageid" arg2="12"/>
</condition>
<condition property="comment" value="appMsg-Outbound">
<equals arg1="messageid" arg2="12"/>
</condition>
</sequential>
</for>
Is this valid? Is there any other way?
Also if i want to go through the loop each time the property comment should get updated with the new value, but since properties are immutable, how do i solve this?
Thanks
The variable task is mutable. It's in the ant contrib library which I see you are already using for the for task.

Resources