?
Solved

passing parameters to Ant

Posted on 2008-06-13
6
Medium Priority
?
8,399 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 2000 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month11 days, 20 hours left to enroll

752 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