aspose file tools*
The moose likes Ant, Maven and Other Build Tools and the fly likes Loops in Ant Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » Ant, Maven and Other Build Tools
Bookmark "Loops in Ant" Watch "Loops in Ant" New topic
Author

Loops in Ant

Jay Damon
Ranch Hand

Joined: Jul 31, 2001
Posts: 282
Is there any way to perform a looping action of any type in Ant? If so, can someone provide an examples? Thanks
Lewin Chan
Ranch Hand

Joined: Oct 10, 2001
Posts: 214
Hi,
I don't believe you can with the core ant tasks, I did have rather an extensive look at this. One thing you have to remember is that.

ant is not a scripting language.

You can of course write you own task and execute some another task multiple times within it...
L


I have no java certifications. This makes me a bad programmer. Ignore my post.
Jay Damon
Ranch Hand

Joined: Jul 31, 2001
Posts: 282
Actually, I am now looking at performing looping tasks using JavaScript. It was relatively easy to get JavaScript enabled and I now have some working JavaScript in my Ant script file. However, I am still trying to work out how Ant targets can call JavaScript and vice versa.
Adam Hardy
Ranch Hand

Joined: Oct 09, 2001
Posts: 566
Hi Jay,
What do you actually want to do?


I have seen things you people would not believe, attack ships on fire off the shoulder of Orion, c-beams sparkling in the dark near the Tennhauser Gate. All these moments will be lost in time, like tears in the rain.
Jay Damon
Ranch Hand

Joined: Jul 31, 2001
Posts: 282
I am working on some Ant scripts designed to be run inside the WebSphere Studio Application Developer (v4.0.3) IDE. Because there are a number of tasks, e.g. refresh, rebuild, etc., that are applicable to all projects, what I want to do is 1) declare all my project names in one place (so I do not have to create task-specific code for each of the refresh, rebuild, etc. tasks), and 2) use a looping mechanism to execute each of these tasks for each project.
I have managed to get JavaScript working inside my Ant scripts and it appears this may solve my problems by 1) declaring the project types and names within JavaScript arrays, and 2) using a for loop within JavaScript to execute Ant targets repeatedly.
However, I am still working on some issues related to the execution of JavaScript from Ant targets and vice versa. But I am fairly confident that I will be able to get the Ant scripts to do what I want now although I still have to work out the details.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16145
    
  21

Welcome to the club. I too have multiple similar subprojects that I'd like to be able to iterate Ant sub-builds over without having to change the master build.xml every time I add one. Even just running over a list from a property would suffice.
Not now, and, as far as I can tell, so time soon. I don't know if if's something the Ant people don't feel a need for or if it's just something they object to


Customer surveys are for companies who didn't pay proper attention to begin with.
Jay Damon
Ranch Hand

Joined: Jul 31, 2001
Posts: 282
Check out Using JavaScript with Ant at:
http://www.sitepen.com/ant/javascript.html
As I indicated, I am currently looking at JavaScript as a solution to this problem. I'm still working out the details but the following Ant script, as an example, works once you add the bsf.jar (Bean Scripting Framework) and js.jar (JavaScript) files to your classpath.
Unfortunately, I am currently locked into Ant v1.3 because I am using IBM WebSphere Studio Application Developer v4.0.3. I think some of my problems would be solved if I could move to Ant v1.4 or v1.5.
Paul Done
Greenhorn

Joined: Jan 03, 2003
Posts: 22
In a recent project I used a different method. In a master buildfile I included an Xslt/Style task which generated another buildfile on the fly and then executed it. This generated buildfile would include a series of tasks derived from a list in an XML config file I had in my source project. This may seem convoluted and obviously depends on having an XML metadata file to drive it in the first place. However, in my case, once set up, it worked really easily - the XML file was actually a J2EE deployment descriptor in my situation.
[ January 07, 2003: Message edited by: Paul Done ]
Jay Damon
Ranch Hand

Joined: Jul 31, 2001
Posts: 282
Paul, can you provide examples of your solution?
Paul Done
Greenhorn

Joined: Jan 03, 2003
Posts: 22
Unfortunately I don't have the orginal buildscripts at hand - this was done for a customer last year - however I have found the XSL which I called from the XLS/Style ant task in the master buildfile - not sure if this is any help?....

...which generated the buildfile....

Not sure if this helps because its very specific to the problem at the time. Basically I was creating a large WAR web component from lots of smaller ones developed by different developers in source control. Each sub project had its own buildfile, but there was not way to automate the build of the master web component from these smaller web components until I used this method (a single web component was required for production, rather than an EAR containing multiple WARs, because the servlet context needed to be shared). In fact, this example didn't use a deployment descriptor as input to the XSL, it used a custom XML config file that was also source controlled - but the principle is the same.
Paul
Jay Damon
Ranch Hand

Joined: Jul 31, 2001
Posts: 282
Thanks, Paul.
Nicolas Brasseur
Ranch Hand

Joined: Nov 09, 2001
Posts: 45
Hello,
I tried to implement Paul's solution, unfortunately I get some problems when using the $ { } characters ...
Here is a code snippet of my stylesheet :

As Paul did I tried to use internal entities replacing the $ { } characters ... the result I get is quite dispointing :

I only get a single dollar sign. I saw Paul was nopt always using the entities in his stylesheet ... is there any rule I am not respecting ?
Thanks in advance
Nicolas
PS : I know my question is more about XSLT than ant but it directly concerns people who are trying to build Ant xml files using XSLT to solve some problems (cfr. my yesterday post)


<a href="http://www.javablackbelt.com/?utm_source=javaranch&utm_medium=signature&utm_campaign=Forum%2BBuzz" target="_blank" rel="nofollow">BlackBeltFactory Communtiy</a> founder - Building better developers<br /><b>Free</b> courses and mock exams
Nicolas Brasseur
Ranch Hand

Joined: Nov 09, 2001
Posts: 45
Actually entities work well when unsed in elements ... and don't when used in attributes ...
will I be forced to use <xsl:element> <xsl:attribute> everywhere or is it a solution to use entities as attributes values ?
Thanks in advance ...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Loops in Ant