conditional property setting - ant

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.

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 condition block

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.

exception when using if statment in ant script

I am trying to compare the value of a properties variable with a string as following
<if>
<equals "${mat.projectName}"="seal">
<then>
When done so, I'm getting following message.
Element type "equals" must be followed by either attribute specifications,">" or
"/>"
I'm using eclipse framework to do this.
Read the manual first:
http://ant.apache.org/manual/Tasks/conditions.html
clearly, from the manual we know for equals:
arg1 First value to test
arg2 Second value to test
So it should be
<if>
<equals arg1="${mat.projectName}" arg2="seal" />
<then>
...
I recommend you to read guides about XML first, and then, Ant's manual.
Update:
<if> task is not provided by Ant; it is provided by Ant-Contrib. So you need <taskdef>.
For example, I have ant-contrib.jar put in my project's lib directory (${basedir}/lib), so I can write the following:
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<pathelement location="lib/ant-contrib.jar"/>
</classpath>
</taskdef>
For more, you can check taskdef's manual page, as well as Ant-contrib's webpage:
http://ant.apache.org/manual/Tasks/taskdef.html
http://ant-contrib.sourceforge.net/
Exactly what you're error message says...
Element type "equals" must be followed by either attribute specifications,">" or "/>"
You want this:
<if>
<equals arg1="${mat.projectName}" arg2="seal"/>
<then>
<yadda, yadda, yadda/>
</then>
</if>
This is XML, so you need parameters with values. Take a look at the equals condition on this page. It takes two parameters.
Notice the format of the <if>. The condition ends with a />. The <then> is a sub-entity of the <if>, and the if clause is a sub-entity of the <then> clause. Notice that you basically indent twice.
If you're doing a not equals condition, it would look like this:
<if>
<not>
<equals arg1="${mat.projectName}" arg2="seal"/>
</not>
<then>
<yadda, yadda, yadda/>
</then>
</if>

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 ;-)

Resources