Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 789
Bharat Sinha Avatar asked Aug 07 '12 06:08

Bharat Sinha


People also ask

How do I run a specific target in ant?

To run the ant build file, open up command prompt and navigate to the folder, where the build. xml resides, and then type ant info. You could also type ant instead. Both will work,because info is the default target in the build file.

What is Antcall in build XML?

Properties are passed to the new target using nested <param> elements. An investigation of the Ant source code reveals that antcall instantiates and calls the ant task using the current buildfile. This means that a new project instance is created and properties work the same as they do for ant .

What is Apache Ant with example?

Apache ANT is a Java based build tool from Apache Software Foundation. Apache ANT's build files are written in XML and they take advantage of being open standard, portable and easy to understand. This tutorial will teach you how to use Apache ANT to automate the build and deployment process in simple and easy steps.


1 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>
like image 127
Kevin Struillou Avatar answered Oct 05 '22 23:10

Kevin Struillou