Use Ant to run a program with command line arguments

My program receives command line arguments. How to pass it when I use Ant?

+53
java ant
Sep 16 '10 at 21:16
source share
5 answers

Extension of Richard Cook's answer .

Here's the ant job 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 of starting 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 call either from the command line, like this:

 ant -Darg0=Hello -Darg1=World run 

Be sure to use the -Darg syntax; if you run this:

 ant run arg0 arg1 

then ant will try to run targets arg0 and arg1 .

+67
Sep 16 '10 at
source share

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

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

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

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

Putting it all together, let's:

 <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 request command line arguments, and the last will not.

+26
Feb 11 2018-12-12T00:
source share

The only effective mechanism for passing parameters to the assembly is to use Java properties:

 ant -Done=1 -Dtwo=2 

The following example shows how you can verify and verify that the expected parameters were passed to 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> 
+10
Sep 16 '10 at 10:40
source share

Can you be more specific about what you are trying to do and how you are trying to do it?

If you are trying to invoke a program using the <exec> task, you can do the following:

 <exec executable="name-of-executable"> <arg value="arg0"/> <arg value="arg1"/> </exec> 
+3
Sep 16 '10 at 21:18
source share

In the end, I made a batch file to extract CLASSPATH from the ant file, and then ran java directly using this:

In my build.xml file:

 <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 is no longer cross-platform, but at least it is relatively easy to use, and you can provide a .bat file that does the same thing as run.sh. This is a very short batch of script. This is not like moving the entire assembly into batch files for a specific platform.

I think this is a shame, not some kind of option in ant, thanks to which you could do something like:

 ant -- arg1 arg2 arg3 

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

0
Oct 12
source share



All Articles