The moose likes Ant, Maven and Other Build Tools and the fly likes Creating for loops in ANT Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Ant, Maven and Other Build Tools
Bookmark "Creating for loops in ANT" Watch "Creating for loops in ANT" New topic

Creating for loops in ANT

Cindy Martin

Joined: Jul 29, 2008
Posts: 10

can we write a build file with a for loop that runs n times ?

For eg., i want to run a for loop for n no of time depends on the value of n. But i dont want to write in java and call in build file...i am just trying with ANT tasks itself todo this... if anyone gives me a hint, or know abt how to write, kindly help me...

Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

Ant doesn't directly support this. It very deliberately doesn't have scripting-like constructs becasue it is not a scripting language. However, you can get the ant-contrib extensions which include this sort of stuff.

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42958
Or you could write your own AntTask that reads a property from the build file, and iterates accordingly. It's not that hard.
Cindy Martin

Joined: Jul 29, 2008
Posts: 10
Thanks Paul and Ulf !! you have given me a hint...

Using ANT Contrib tasks , i just wrote the simple loop

This code results in, printing the values one by one. But the size is fized and i need to give all the inputs in the list attribute...Is there any way that i can specify the list = No of values (n) and iterate it by the loop ( i= i+1). I tried several possibilities, but i cud not get it. Can anyone help me ?

Peter Johnson

Joined: May 14, 2008
Posts: 5852

Try this:

then run ant as:

ant -Dmy.list=1,2,3,4,5

Or you can set the my.list property in a properties file or in another location within the script.

JBoss In Action
I agree. Here's the link:
subject: Creating for loops in ANT
jQuery in Action, 3rd edition