Solved

passing parameters to Ant

Posted on 2008-06-13
6
8,343 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
strCopies  challenge 17 74
maven java path setting 5 50
advertisement module in core php 4 94
Java Server Faces parameter pass? 6 24
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…
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 …
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…
This video teaches viewers about errors in exception handling.

744 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

9 Experts available now in Live!

Get 1:1 Help Now