Link to home
Start Free TrialLog in
Avatar of dportabella
dportabella

asked on

passing parameters to Ant

Passing parameters from the command line to your ant build file.

You can pass parameters from the command line to your ant build file by using properties.
Using ant -Dname=value lets you define values for properties on the Ant command line.
These properties can then be used within your build file as any normal property: ${name} will put in value.


However,
Let's say that I have the following java program:
++++++++++++++
class Test {
  public static void main(String argv[]) {
    for (int i = 0; i < argv.lenght;i++)
      System.out.println("param " + i + ": " + argv[i]);
  }
}
++++++++++++++

and I have the following ant file:
++++++++++++++
<project basedir=".">
      <target name="run">
            <java fork="true" classname="Test"/>
      </target>
</project>
++++++++++++++

How do I need to modify the ant file so that executing "ant run one two three" from the command line, produces:
++++++++++++++
param 1: one
param 2: two
param 3: three
++++++++++++++

(and it works for any number of parameters)



Many thanks,
DAvid

Avatar of Ajay-Singh
Ajay-Singh

User nested <arg> tags
http://ant.apache.org/manual/CoreTasks/java.html
 
<project basedir=".">
      <target name="run">
            <java fork="true" classname="Test">
                <arg value="one" />
                <arg value="two" />
                <arg value="three" />
            </java>
      </target>
</project>
Avatar of dportabella

ASKER

Alay-Singh,

In your example, you need to define that there are exactly three parameters.

The ant file should work for any number of parameters.
That is, the number of parameters is unknown at the time of writing the ant file.

e.g. the same ant file should work for these cases:
ant run one two three
ant run one two three four
ant run one two three four five ...


DAvid

In that case, you can define system argument and use that in ant script
 
ant -Dargs="one two three" run
 
<project basedir=".">
      <target name="run">
            <java fork="true" classname="Test">
                <arg value="${args}" />
            </java>
      </target>
</project>
executing this:
ant -Dargs="one two three" run

results in
param 0: one two three


instead of:
param 0: one
param 1: two
param 2: three


ASKER CERTIFIED SOLUTION
Avatar of Ajay-Singh
Ajay-Singh

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial