Quantcast
Viewing all articles
Browse latest Browse all 26

Apache Ant – for loop example for reading a text file

The Ant-Contrib library also provides u a <for> task to do iteration. Here is an simple Ant example which read a text files and print it to the Ant build output.

1. This is the structure of our Ant project.

ant-for-example
  | - ant-lib
  |    | - ant-contrib-1.0b3.jar
  | - build.xml
  | - text.txt

 

2. text.txt

1. Hello World
2. Welcome to Eureka!
3. Goodbye

 

3. build.xml

<project name="ant-for-example" default="read" basedir=".">

  <!-- Load the ant contrib lib -->
  <taskdef resource="net/sf/antcontrib/antlib.xml">
    <classpath>
      <pathelement location="${basedir}/ant-lib/ant-contrib-1.0b3.jar"/>
    </classpath>
  </taskdef>

  <target name="read">
    <loadfile property="textFile" srcfile="${basedir}/text.txt"/>
    <for param="line" list="${textFile}" delimiter="${line.separator}">
      <sequential>
        <echo message="@{line}"/>
      </sequential>
    </for>
  </target>
</project>

 

4. Try it out.
Image may be NSFW.
Clik here to view.
ant-for-example

 

Done =)

Reference:


Filed under: Ant Tagged: Ant, Ant-Contrib Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.

Viewing all articles
Browse latest Browse all 26

Trending Articles