Use Ant for Running Program with Command Line Arguments

Use Ant for running program with command line arguments

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.

Java, main command line arguments and Ant build file

Here's a bit of a change in the <java> task:

<target name="run" depends="jar">
<java jar="${jar.dir}/${ant.project.name}.jar" fork="true">
<arg value="${file1}"/>
<arg value="${file2}"/>
</java>
</target>

Now, you have two new parameters: file1 and file2 You can pass those from the command line like this:

$ ant -Dfile1=foo.txt-Dfile2=bar.txt run

Ant takes a list of targets, and you are executing target run. The -D parameters are the properties you're passing to your program.

I recommend that you put default parameters in your program like this:

<target name="run" depends="jar">
<property name="file1" value="bar.txt"/>
<property name="file2" value="foo.txt"/>
<java jar="${jar.dir}/${ant.project.name}.jar" fork="true">
<args value="${file1}"/>
<args value="${file2}"/>
</java>
</target>

This way, if you don't pass any parameters, it will use your default parameters of foo.txt and bar.txt. Remember that properties set on the command line with the -D parameter override the value of those properties set in the build.xml file itself. Once a property is set, it can never be changed.

pass line arguments to main from ant

If the number of arguments is known you can use the -D option to pass in the argument and then in your "run" task target use 'arg' to pass it to jar file like

<target name="run" depends="jar">

<java jar="${jar.dir}/${ant.project.name}.jar" fork="true">
<arg value="${arg1}"/>
<arg value="${arg2}"/>
<arg value="${arg3}"/>
<arg value="${arg4}"/>
<arg value="${arg5}"/>
</java>
</target>

and you should invoke your ant file as

ant -Darg1=1 -Darg2=2 -Darg3=3 -Darg4=4 -Darg5=5

Pass Command Line arguments to Apache Ant build file from DITAOT custom plugin

So finally I figured it out.

dita -f html5 -i /test.dita -Done=abc

will pass "one" as a parameter to the build.xml file. In build.xml file you will access the argument by following. No changes needed in plugin.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:if="ant:if" xmlns:unless="ant:unless" name="dita2html5-test" default="get-parameters">
<target name="get-parameters">
<echo>Embed another1:${one}</echo>
<echo message="print value:${args.artlbl}"/>
</target>

Passing command line arguments to Java via ant build script

I'm not sure exactly how you want to pass these values, but there are several mechanisms:

  • Use <sysproperty> to pass system properties you need to set:
  • Use <arg> to pass command line arguments to your Java class
  • Use <jvmarg> to pass arguments to your Java command itself
  • If you fork your Java task, you can also set environment variables too. These are ignored if you don't fork the Java task

This:

 $ foo=bar; java -Xlingc com.example.foo.bar -Dsys1=fu -Dsys2=barfu -arg1 -arg2 bar

Becomes:

<java classname="com.example.foo.bar"
fork="true">
<env key="foo" value="bar"/>
<sysproperty key="sys1" value="fu"/>
<sysproperty key="sys2" value="barfu"/>
<jvmarg value="-Xlingc"/>
<arg value="-arg1"/>
<arg value="-arg2"/>
<arg value="bar"/>
</java>

Hope that example helps

How to run a specific junit test in ant with parameter?

Introduce property which defines test name you want to execute:

<property name="unit.test" value="*.java" />

Use this property in your batchtest:

<batchtest fork="yes" todir="${output.test.dir}">
<fileset dir="${source.test.dir}" includes="**/${unit.test}"/>
</batchtest>

Pass value for this property to ant:

ant test -Dunit.test=A50Test

ANT: Use , in arg without expansion to multiple arguments

Issue has been resolved. I was so focused on the issue being in ant, that I didn't take the time to test how DOS like command lines interpret the command-line arguments.

from Window command line, I ran test.cmd a,c,b and see that the command argument was split, therefore, the issue is not related to ant. so now I just need to figure out how to force ANT to quote the arguments.



Related Topics



Leave a reply



Submit