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 に答える