pass arguments to apache-ant exec task based on the variable's value

Viewed 11206

I am only using apache-ant and not ant-contrib

I have an ant target

<target name="stop" depends="init" >
...
</target>

In which i want to invoke exec task.

If the value of a variable HOST_NAME is all

<exec executable="${executeSSH.shell}" >
    <arg value="-h ${HOST_NAME}" />
    <arg value="-i ${INSTANCE}" />
    <arg value="-w 10" />
    <arg value="-e ${myOperation.shell} " />
    <arg value=" -- " />
    <arg value="${INSTANCE} ${USERNAME} ${PASSWORD}" />
</exec>

If the value of a variable HOST_NAME is anything else

<exec executable="${executeSSH.shell}">
    <arg value="-h ${HOST_NAME}" />
    <arg value="-i ${INSTANCE}" />
    <arg value="-e ${myOperation.shell} " />
    <arg value=" -- " />
    <arg value="${INSTANCE} ${USERNAME} ${PASSWORD}" />
</exec>

But i would like to write only one task and not to repeatexec. I have used HOST_NAME parameter but what to do about the second parameter -w 10 which is different in both calls.

I have tried a couple of ways by searching on SO by using condition and if else but nothing seems to be applicable for exec or arg.

2 Answers

You can use the condition task:

<condition property="my.optional.arg" value="-w 10" else="">
    <equals arg1="${HOST_NAME}" arg2="all" />
</condition>

<exec executable="${executeSSH.shell}" >
    <arg value="-h ${HOST_NAME}" />
    <arg value="-i ${INSTANCE}" />
    <arg line="${my.optional.arg}" />
    <arg value="-e ${myOperation.shell} " />
    <arg value=" -- " />
    <arg value="${INSTANCE} ${USERNAME} ${PASSWORD}" />
</exec>
Related