13

In perl we use <FileDescriptor> to read data line by ilne from a file. How to do the same using ant script.

tshepang
  • 12,111
  • 21
  • 91
  • 136
rashok
  • 12,790
  • 16
  • 88
  • 100

4 Answers4

29

You can do that using the loadfile task in combination with the for task from ant-contrib (you will have to download and install ant-contrib).

<project name="test" default="compile">

  <taskdef resource="net/sf/antcontrib/antcontrib.properties">
    <classpath>
      <pathelement location="path/to/ant-contrib.jar"/>
    </classpath>
  </taskdef>

  <loadfile property="file" srcfile="somefile.txt"/>

  <target name="compile">
    <for param="line" list="${file}" delimiter="${line.separator}">
      <sequential>
        <echo>@{line}</echo>
      </sequential>
    </for>
  </target>

</project>
Lesmana
  • 25,663
  • 9
  • 82
  • 87
  • I cant install `ant-contrib` in my application. Could you please tell me how to read only the first line of a file, without using `for` – rashok Mar 28 '11 at 13:49
  • @rajaashok you can use [`headfilter`](http://ant.apache.org/manual/Types/filterchain.html#headfilter) inside `loadfile` – Lesmana Mar 28 '11 at 13:52
5

Just had to do that myself, actually the for + line.separator solution is flawed because :

  • it only works if the file EOLs match the platform EOL
  • it discards empty lines

Here is another (better) solution based on the previous example :

<project name="test" default="compile">

  <taskdef resource="net/sf/antcontrib/antcontrib.properties">
    <classpath>
      <pathelement location="path/to/ant-contrib.jar"/>
    </classpath>
  </taskdef>

  <loadfile property="file" srcfile="somefile.txt"/>

  <target name="compile">
    <for param="line">
      <tokens>
        <file file="${file}"/>
      </tokens>
      <sequential>
        <echo>@{line}</echo>
      </sequential>
    </for>
  </target>

</project>
mat007
  • 905
  • 8
  • 16
  • Would you mind explaining where the come from? I cannot find it in the ant documentation. Is it used to split by line? – mgouin Feb 14 '19 at 03:54
  • 1
    It's a resource collection, see http://ant.apache.org/manual/Types/resources.html#tokens – mat007 Feb 15 '19 at 07:18
2

The example using tokens did not work for me. In my scenario I was looking to simply print a README file while retaining the blank lines. Here is what I did.

<taskdef name="if-contrib" classname="net.sf.antcontrib.logic.IfTask" classpath="${basedir}/lib/ant/ant-contrib-1.0b3.jar" />
<taskdef name="for-contrib" classname="net.sf.antcontrib.logic.ForTask" classpath="${basedir}/lib/ant/ant-contrib-1.0b3.jar" />
<taskdef name="var-contrib" classname="net.sf.antcontrib.property.Variable" classpath="${basedir}/lib/ant/ant-contrib-1.0b3.jar" />
<target name="help">
    <for-contrib param="line">
        <tokens>
            <file file="README.txt" />
        </tokens>
        <sequential>
            <var-contrib name="line.length" unset="true" />
            <length string="@{line}" property="line.length" />
            <if-contrib>
                <equals arg1="${line.length}" arg2="0" />
                <then>
                    <echo>
                    </echo>
                </then>
                <else>
                    <echo>@{line}</echo>
                </else>
            </if-contrib>
        </sequential>
    </for-contrib>
</target>
lokivog
  • 21
  • 1
-1

Try This it should be work.....

<project name="test" default="compile">
 <loadfile property="file" srcfile="Help.txt"/>
   <target name="compile">
    <echo>${file}</echo> 
   </target>
</project>
Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
Aniket Patil
  • 87
  • 1
  • 3