59

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

Victoria Seniuk
  • 1,384
  • 3
  • 14
  • 23

5 Answers5

78

Extending Richard Cook's answer.

Here's the ant task 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>

Here's the task to run a Java program from a .jar file:

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

You can invoke either from the command line like this:

ant -Darg0=Hello -Darg1=World run

Make sure to use the -Darg syntax; if you ran this:

ant run arg0 arg1

then ant would try to run targets arg0 and arg1.

emory
  • 10,725
  • 2
  • 30
  • 58
30

If you do not want to handle separate properties for each possible argument, I suggest you'd use:

<arg line="${args}"/>

You can check if the property is not set using a specific target with an unless attribute and inside do:

<input message="Type the desired command line arguments:" addProperty="args"/>

Putting it all together gives:

<target name="run" depends="compile, input-runargs" description="run the project">
  <!-- You can use exec here, depending on your needs -->
  <java classname="Main">
    <arg line="${args}"/>
  </java>
</target>
<target name="input-runargs" unless="args" description="prompts for command line arguments if necessary">
  <input addProperty="args" message="Type the desired command line arguments:"/>
</target>

You can use it as follows:

ant
ant run
ant run -Dargs='--help'

The first two commands will prompt for the command-line arguments, whereas the latter won't.

ofavre
  • 4,488
  • 2
  • 26
  • 23
13

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
  • 76,015
  • 10
  • 139
  • 185
  • How does xml look like when I don't know how many args I will have? – Victoria Seniuk Sep 17 '10 at 12:26
  • 1
    What if number of args isn't constant? – Victoria Seniuk Sep 17 '10 at 12:37
  • 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 Sep 17 '10 at 21:45
  • 1
    Really helpful to have an entire file as a working exercise - we never did work out why parameter passing didn't work in our original file, but successfully modified the working file to include all the needed functionality without breaking the parameters... – Francis Norton Nov 20 '14 at 16:47
  • What if I need just pass sort of switcher (e.g. -f in bash commands), i.e. option without value? Looks like '-Drelease' or '-Drelease=' triggers error re missing property value – monitor Sep 05 '17 at 08:42
  • @monitor You cannot easily create your own flags (example: -f) for the ANT process. Setting Java properties is an approximation to parameter passing into the ANT script. – Mark O'Connor Sep 07 '17 at 08:55
4

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
  • 32,523
  • 5
  • 46
  • 71
0

What I did in the end is make a batch file to extract the CLASSPATH from the ant file, then run java directly using this:

In my build.xml:

<target name="printclasspath">
    <pathconvert property="classpathProp" refid="project.class.path"/>
    <echo>${classpathProp}</echo>
</target>

In another script called 'run.sh':

export CLASSPATH=$(ant -q printclasspath | grep echo | cut -d \  -f 7):build
java "$@"

It's no longer cross-platform, but at least it's relatively easy to use, and one could provide a .bat file that does the same as the run.sh. It's a very short batch script. It's not like migrating the entire build to platform-specific batch files.

I think it's a shame there's not some option in ant whereby you could do something like:

ant -- arg1 arg2 arg3

mpirun uses this type of syntax; ssh also can use this syntax I think.

Hugh Perkins
  • 7,975
  • 7
  • 63
  • 71