[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

passing parameters to Ant

Posted on 2008-06-13
6
Medium Priority
?
8,418 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues undeā€¦
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

650 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