Reputation: 589
I need to compare list of related environments and perform the operation if any of the environment is matching with the passed argument environment. The below code compares each value at time. I don't want to write for each of the environment as i need to perform the same operation of a group of envs.
How can i compare all the environments (DEV1, DEV2, DEV3) together?
<if>
<equals arg1=“${env}” arg2="DEV" /> <!-- Need to compare with DEV1, DEV2, DEV3 -->
<then>
<echo>Dev related env </echo>
</then>
<elseif>
<equals arg1="${env}" arg2=“TST” /> <!-- Need to compare with TST, E2E, UAT -->
<then>
<echo>Test related env </echo>
</then>
</elseif>
Upvotes: 1
Views: 1611
Reputation: 4614
First, I would highly recommend avoiding the use of ant-contrib (the 3rd party library that provides if/else blocks, for loops, etc) whenever possible. This sort of thing is best done using target-level conditions and dependencies native to Ant. Here's an example of how this can be accomplished.
<target name="build" depends="development,test" />
<target name="init">
<condition property="DEV">
<or>
<equals arg1="${env}" arg2="DEV1" />
<equals arg1="${env}" arg2="DEV2" />
<equals arg1="${env}" arg2="DEV3" />
</or>
</condition>
<condition property="TST">
<or>
<equals arg1="${env}" arg2="TST" />
<equals arg1="${env}" arg2="E2E" />
<equals arg1="${env}" arg2="UAT" />
</or>
</condition>
</target>
<target name="development" depends="init" if="DEV">
<echo message="Dev related env" />
</target>
<target name="test" depends="init" if="TST">
<echo message="Dev related env" />
</target>
With the above build script, the user simply calls the build
target.
build
depends on both development
and test
so the script will jump to these and check their dependenciesinit
, which has no dependencies, so the script will proceed to run init
DEV
is set to true if the property env
has one of the 3 development values, and likewise TST
is set to true if env
has a test valuedevelopment
and test
targets, but only runs either one if its respective condition is trueSide note: if your env
value follows a predictable pattern (i.e. DEV1 - DEV70), you can use the contains
condition to simplify things.
<condition property="DEV">
<contains string="${env}" substring="DEV" />
</condition>
Upvotes: 4