• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Apache Ant - Doubt

Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

Got an issue with Apache ant. I wonder why nobody else in the whole world has faced this issue.

Using Ant, I would want to read a file (its not a property file) which contains the values as -


I want to read it line by line and get individual dir names in the ant code. There is no option even in ant contrib to do a simple file read line by line either using <for> or <foreach>. Even if I try writing a custom task, it would still be a issue as the vars are all immutable and i am not sure how many lines are there in my file.

Any suggestions please ?

Cheers !
Posts: 21737
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, while the general idea is fairly common, your specific need is unusual.

Ant isn't a programming or scripting language system. Its omission of iterative constructs was deliberate, though there are several ways of giving it that ability, including via the use of JavaScript.

For what it's worth, since your data format is so distinct, I'd personally do a custom ant task, just because they're simple to do and it would make the project overall more elegant.

I'm not sure why the variability of the number of lines would matter to a custom task, since it would normally pick them up via a readline loop. Nor do I understand what "immutable" means here, since Ant properties are supposed to be immutable. And, without actually RTFMing, I'm pretty sure an Ant task can define properties. After all, the builtin ones do.
Dilish Kuruppath
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks tim for the reply!

I was just able to write a good custom task to achieve it. The reason for the skepticism was my odd requirement to return back the values in the same variables for every line read and then to process it - as the vars are immutable, the need to change its values for every new line read.. hence was not possible

The solution I found was quite simple and decent here. Did it in the same lines as the for loop is written - i.e. wrote a custom task to read each line and set it to macroinstance and made it to execute :-)

public void execute()
// some validations
// perform the iterations

private void doTheTasks() {
errorCount = 0;
taskCount = 0;

// Create a macro attribute
if (macroDef.getAttributes().isEmpty()) {
MacroDef.Attribute Asource= new MacroDef.Attribute();

MacroDef.Attribute Adest= new MacroDef.Attribute();

// Try reading the file
if (filename != null) {
// read file line by line and process
}// end if
}// end doTheTasks
I love a woman who dresses in stainless steel ... and carries tiny ads:
Java file APIs (DOC, XLS, PDF, and many more)
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!