tags:

views:

124

answers:

3

My program getting command line arguments. How can I pass it when I use Ant?

+1  A: 

Can you be a bit more specific about what you're trying to do and how you're trying to do it?

If you're attempting to invoke the program using the <exec> task you might do the following:

<exec executable="name-of-executable">
  <arg value="arg0"/>
  <arg value="arg1"/>
</exec>
Richard Cook
Like: ant run arg0 arg1
Victoria
+2  A: 

Extending Richard Cook's answer ur ant task will look like

to run any program (including but not limited to java programs)

  <target name="run">
    <exec executable="name-of-executable">
      <arg value="${arg0}"/>
      <arg value="${arg1}"/>
    </exec>
  </target>

to run a java program from a jar file

  <target name="run-java">
    <java executable="path for jar">
      <arg value="${arg0}"/>
      <arg value="${arg1}"/>
    </java>
  </target>

and u call it like

ant -Darg0=Hello -Darg1=World run

If u tried

ant run arg0 arg1

then ant would try to run targets arg0 and arg1.

emory
Is the "name-of-executable" the path for jar?
Victoria
The exec task (http://ant.apache.org/manual/Tasks/exec.html) will run any arbitrary program. The java task (http://ant.apache.org/manual/Tasks/java.html) will execute a java program.
emory
A: 

The only effective mechanism for passing parameters into a build is to use Java properties:

ant -Done=1 -Dtwo=2

The following example demonstrates how you can check and ensure the expected parameters have been passed into the script

<project name="check" default="build">

    <condition property="params.set">
        <and>
            <isset property="one"/>
            <isset property="two"/>
        </and>
    </condition>

    <target name="check">
        <fail unless="params.set">
        Must specify the parameters: one, two
        </fail>
    </target>

    <target name="build" depends="check">
        <echo>
        one = ${one}
        two = ${two}
        </echo>
    </target>

</project>
Mark O'Connor
Emory beat me to it :-)
Mark O'Connor
How does xml look like when I don't know how many args I will have?
Victoria
What if number of args isn't constant?
Victoria
if number of args is not constant, then probably the best u can do is ant -Dvarargs="Hello World. It is Good to Meet You!" run
emory