Bharat Sinha
Bharat Sinha

Reputation: 14373

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

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.

Upvotes: 11

Views: 12091

Answers (2)

Kevin Struillou
Kevin Struillou

Reputation: 914

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>

Upvotes: 12

Tapemaster
Tapemaster

Reputation: 487

Try using macrodef. The following example is not tested.

<macrodef name="callSSH">
    <element name="extArgs" optional="y"/>
    <sequential>
        <exec executable="${executeSSH.shell}" >
            <arg value="-h ${HOST_NAME}" />
            <arg value="-i ${INSTANCE}" />
            <extArgs/>
            <arg value="-e ${myOperation.shell} " />
            <arg value=" -- " />
            <arg value="${INSTANCE} ${USERNAME} ${PASSWORD}" />
        </exec>
    </sequential>
</macrodef> 
<target name="stop" depends="init" >
    <if>
       <equals arg1="${HOST_NAME}" arg2="all"/>
        <then>
            <callSSH>
                <extArgs>
                    <arg value="-w 10" />
                </extArgs>
            </callSSH>
        </then>
        <else>
            <callSSH>
                <extArgs/>
            </callSSH>
        </else>
    </if>
</target>

Or if you don't use contribs:

<target name="sshExecWithHost" if="HOST_NAME"> 
    <callSSH>
        <extArgs>
            <arg value="-w 10" />
        </extArgs>
    </callSSH>
</target>

<target name="sshExecNoHost" unless="HOST_NAME">
    <callSSH/>
</target>

<target name="sshSwitch" depends="sshExecNoHost,sshExecWithHost">
</target>

<target name="stop" depends="init,sshSwitch" >
</target>

Upvotes: 6

Related Questions