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.
Related
We are using Ant 1.8. I am not an Ant developer but I have to pretend sometimes.
A new property, ${noReportDSUpgrade}, is intended to be "true" or "false".
By default it is empty (not exist?) which is "false" for our purposes.
If this property is empty it should be set to "false".
A command line using this parameter should set it to true.
1) How do I set ${noReportDSUpgrade} to false if empty and true if supplied?
2) For the target, how to execute only if false?
I have tried several suggestions I've found but can't get it to work.
At the beginning of the script:
<target name="init">
<antcall target="setnoReportDSUpgradeProperty"/>
Further down:
<target name="setnoReportDSUpgradeProperty">
<condition>
<or>
<equals arg1="${noReportDSUpgrade}" arg2=""/>
<not>
<isset property="false"/>
</not>
</or>
</condition>
<echo message="noReportDSUpgrade set to ${noReportDSUpgrade}"/>
</target>
Here's how to set a default property value in Ant:
<property name="noReportDSUpgrade" value="false" />
That's it! Properties are immutable in Ant, so if you set a value via command line or earlier in the script, subsequent <property> tasks won't change it. This won't account for the property being set to a blank value (i.e. ""), but as a general good practice, try to avoid setting properties to blank.
Even though I don't think you need a <condition> task for your goals here, I feel I should clear some things up in your example. The <condition> task doesn't affect the <target> that it's nested in; it simply sets a property, specified by the property attribute. Additionally, the isset condition's property attribute is used to point to the name of the property you're checking, not the value.
<condition property="noReportDSUpgrade">
<or>
<equals arg1="${noReportDSUpgrade}" arg2=""/>
<not>
<isset property="noReportDSUpgrade" />
</not>
</or>
</condition>
But like I said above, don't use that unless you really need to check for a blank value for some reason. Just use <property>.
As for running targets conditionally, the <target> block supports if and unless attributes that control whether or not the entire thing runs. This can be a bit confusing because there are two modes in which this operates.
<target name="myTarget" if="myCondition">
<echo message="Running myTarget" />
</target>
The above target will run if myCondition is set (not if its value is true). So if it evaluates to "true", "false", "asdf", or just blank, the target will still run. Conversely, if we used the unless attribute, it wouldn't run if myCondition is set to anything. This is usually convenient for when you're using the <condition> task to set your properties (since <condition> does not set a value if the boolean evaluates to false).
<target name="myTarget" if="${myCondition}">
<echo message="Running myTarget" />
</target>
Notice the ${} around myCondition. When you expand the property like this, Ant will only run the target if the property's value is "true", "on", or "yes".
Lastly, you typically don't need to make a separate target just for setting conditions. In relatively simple scripts, you can just use the implicite root target (i.e. put the tasks at root level outside of all other targets).
In short, here's the simplest way to write your script.
<project name="myProject">
<property name="noReportDSUpgrade" value="false" />
<target name="myTarget" if="${noReportDSUpgrade}">
<echo message="Running myTarget" />
</target>
</project>
If you really need an initialization target (note the depends attribute):
<project name="myProject">
<target name="init">
<property name="noReportDSUpgrade" value="false" />
</target>
<target name="myTarget" if="${noReportDSUpgrade}" depends="init">
<echo message="Running myTarget" />
</target>
</project>
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"/>
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.
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 ;-)
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.