Reputation: 13573
I have an ant build.xml
file which contains the following snippet:
<condition property="apiUrl" value="apiUrl1">
<and>
<equals arg1="${area}" arg2="area1"/>
<equals arg1="${env}" arg2="stage"/>
</and>
</condition>
<condition property="apiUrl" value="apiUrl2">
<and>
<equals arg1="${area}" arg2="area1"/>
<equals arg1="${env}" arg2="develop"/>
</and>
</condition>
As you can see from above, <equals arg1="${area}" arg2="area1"/>
is checked twice, and the logic of the snippet is equivalent to the pseudo code:
if (${area} == 'area1' and ${env} == 'stage') {
apiUrl = 'apiUrl1'
}
if (${area} == 'area1' and ${env} == 'develop') {
apiUrl = 'apiUrl2'
}
How can I change build.xml
so that its logic becomes the following nested condition?
if (${area} == 'area1') {
if (${env} == 'stage') {
apiUrl = 'apiUrl1'
}
if (${env} == 'develop') {
apiUrl = 'apiUrl2'
}
}
My ant version is 1.10.3
.
Upvotes: 0
Views: 657
Reputation: 4614
The reason this seemingly minor change can seem so awkward in Ant is because while the conditional setting of properties is simply controlled with the condition
task, the conditional flow of logic is controlled at the target
level. Thus, if you want certain steps to run or be skipped depending on a condition, you'll have to create a separate target that first checks the condition and then tells your main target whether or not it should run.
<target name="setApiUrl" depends="checkArea" if="isArea1">
<condition property="apiUrl" value="apiUrl1">
<equals arg1="${env}" arg2="stage"/>
</condition>
<condition property="apiUrl" value="apiUrl2">
<equals arg1="${env}" arg2="develop"/>
</condition>
</target>
<target name="checkArea">
<condition property="isArea1">
<equals arg1="${area}" arg2="area1"/>
</condition>
</target>
Upvotes: 1
Reputation: 2734
you can achieve that using script
instead of condition
task like this:
<project default="init" name="My Project">
<property name="area" value="area1" />
<property name="env" value="develop" />
<target name="init">
<script language="javascript">
if (project.getProperty('area') == 'area1') {
if (project.getProperty('env') == 'stage') {
project.setProperty('apiUrl', 'apiUrl1');
}
if (project.getProperty('env') == 'develop') {
project.setProperty('apiUrl', 'apiUrl2');
}
}
</script>
<echo>${apiUrl}</echo>
</target>
</project>
Upvotes: 0