13

在 perl 中,我们使用<FileDescriptor>ilne 从文件中读取数据行。如何使用 ant 脚本做同样的事情。

4

4 回答 4

29

您可以将此任务与ant-contribloadfile中的任务结合使用(您必须下载并安装 ant-contrib)。for

<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>
于 2011-03-28T10:42:06.333 回答
5

自己必须这样做,实际上 for + line.separator 解决方案是有缺陷的,因为:

  • 仅当文件 EOL 与平台 EOL 匹配时才有效
  • 它丢弃空行

这是基于前面示例的另一个(更好的)解决方案:

<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>
于 2012-04-02T11:06:14.120 回答
2

使用令牌的示例对我不起作用。在我的场景中,我希望在保留空白行的同时简单地打印一个 README 文件。这就是我所做的。

<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>
于 2012-08-22T20:44:16.593 回答
-1

试试这个它应该是工作......

<project name="test" default="compile">
 <loadfile property="file" srcfile="Help.txt"/>
   <target name="compile">
    <echo>${file}</echo> 
   </target>
</project>
于 2013-05-07T09:54:32.450 回答