aspose file tools*
The moose likes Ant, Maven and Other Build Tools and the fly likes reading a csv file 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 "reading a csv file in ant" Watch "reading a csv file in ant" New topic
Author

reading a csv file in ant

Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29249
    
139

I'm trying to figure out the easiest way to read in a CSV (comma delimited) file in Ant.

I know I can
1) use the load file task to read the whole file into a property
2) use Ant-Contrib's loop to split it into lines (based on the new line character)
3) use reg exps to parse it into the fields per line

This seems like a common operation though, so I'm wondering if anyone knows of a better way. Like a task that takes care of two or more of these steps.

(The build file I am working on is a generic one which is why I am reading in CSV information. I do recognize this complexity shouldn't be in a "normal" build script.)


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
Any reason why you are not using Java code to read and parse the file?


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29249
    
139

I need to call an existing Ant macrodef that we wrote with the results. The current code calls this macrodef with a bunch of properties. I want to externalize these properties into a CSV.

We do have a number of Ant tasks and I am comfortable with creating more. And I do have custom Java Ant tasks that call built in targets. I don't know how (or if it is possible) to call a custom macrodef from Java though.

Between the two of these, it seems easier to read the file in Ant. I do know a way to do it. I just imagine there might be a better way.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29249
    
139

For what it's worth, I went with:

I know I can
1) use the load file task to read the whole file into a property
2) use Ant-Contrib's loop to split it into lines (based on the new line character - need to use UNIX escape sequence for this so Ant didn't treat "\n" as to characters when passing it to the tokenizer)
3) write a custom task (using split) to set properties with the parsed values for each line blanking out any unused ones

This doesn't feel awkward. Steps 1 & 2 are done with Ant library code. And step 3 is reusable within our project if I have to do this again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: reading a csv file in ant
 
Similar Threads
Output of telnet task
Reading a CSV file--> Fastest way
Can I debug in IDEA using ant to build?
[Groovy] Ant Scripting
Reading csv file