programmers fix the issue. Good programmers find the solution.

Parsing data file using Ant

A typical operation in Ant scripts is property file loading. There are several ways to load them.

Most of us familiar with these tasks. But question here is, What if required to parse a custom data file?

Ant’s answer to this question is loadfile task with nested filterchain task.

loadfile task is a simple task which loads the input file specified by “srcFile” attribute and sets it to a property specified by “property” attribute.

<loadfile srcFile="sample.txt" property="aProperty"/>

In this example, contents of “sample.txt” file will be set to “aProperty”. In this form, the loadfile task looks very simple. Because much of its power comes from nested filterchain task. filterchain is a group of ordered filterreaders from built-in filters or custom filters.

<loadfile srcFile="sample.txt" property="aProperty">
      <filter1 .../>
      <filter2 .../>
      <filterN .../>

Here are few interesting built-in filters.

HeadFilter Reads the first few lines.

LineContains Includes only those lines that contain all the user-specified strings.

PrefixLines Prefix to every line.

DeleteCharacters Deletes specified characters.

TokenFilter is a powerful filter. It tokenizes the input string and applies filters at the token level. It has its own sets of sub filters. Regexp search and replace can be implemented using ContainsRegex and ReplaceRegex sub filters. So it is the right choice for complex parsing.

More info about filterchain and filters can be found at

Following example illustrates the power of Ant file parsing.

Lets say sample.txt is as follows,


and to find words contain character ‘a’ or ‘b’ or ‘c’, following script can be used.

<project name="AntExample" default="parse" basedir=".">
   <target name="parse" description="Parsing sample.txt">
      <loadfile srcFile="sample.txt" property="aProperty">
            <containsregex pattern=".*[abc].*" flags="gi"/>


Here the filterchain has one containsregex filter with RegExp pattern “.*[abc].*” and flags(global search and ignore case).

Output is,

Buildfile: build.xml

[echo] quick
[echo] brown
[echo] lazy

Total time: 0 seconds

Since it is an example, parsed text is printed in the console. But it can be an input for other tasks to implement complex build process.

Your comments are greatly appreciated.


About these ads

Comments on: "Parsing data file using Ant" (2)

  1. hi Sujee, what if you want to parse different files that all have the same property, e.g. aProperty, but different values?
    file 1 –> aProperty=value1
    file 2 –> aProperty=value2

    value n

    since properties in Ant are immutable, aProperty is set to value1 the first time and I don’t know how to get value2…n

    do you have any suggestions? thanks in advance

  2. Informative, useful and clear post. Thanks!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

%d bloggers like this: