Solved

passing parameters to Ant

Posted on 2008-06-13
6
8,348 Views
Last Modified: 2010-04-21
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

0
Comment
Question by:dportabella
  • 3
  • 3
6 Comments
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 21780371
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>
0
 

Author Comment

by:dportabella
ID: 21780463
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

0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 21780564
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>
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:dportabella
ID: 21780685
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


0
 
LVL 23

Accepted Solution

by:
Ajay-Singh earned 500 total points
ID: 21780713
I am sorry, the line
 
 <arg value="${args}" />

 
should be changed to
 
 <arg line="${args}" />
0
 

Author Closing Comment

by:dportabella
ID: 31466978
Here there is another related question:
http://www.experts-exchange.com/Programming/Languages/Java/Q_23483418.html

Thanks,
DAvid
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Android studio getdrawable(int) is deprecated 4 41
difference of if loops 23 41
varialbe initialization 11 30
MySqlDump not dumping triggers 1 17
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now