【问题标题】:How to read and process stdout values in ant - exec?如何在 ant - exec 中读取和处理标准输出值?
【发布时间】:2019-03-20 22:19:48
【问题描述】:

使用可执行文件验证文件。 我的 ant 脚本目标为:

<target name="xtest" depends="xyz" description="Additional check">
    <exec executable="${xtest.exe}" failonerror="true" resultproperty="retVal">
        <arg value="${inputfile.dat}" />
    </exec>
    <echo>Returned: ${retVal}</echo>
</target>

控制台输出:

xtest:
     [exec] Errors:          3
     [exec] Warnings:        1
     [exec] Infos:           0
     [exec] Total:           4
     [echo] Returned: 0

即使检测到错误,可执行文件的退出代码 (${retVal}) 也是 0。 如果错误超过0,我想终止进程。

如何读取第一个输出行 ([exec] Errors: 3) 并解析上面示例中的 3 值并终止进程?

【问题讨论】:

    标签: java eclipse build ant


    【解决方案1】:

    使用resultproperty,您可以将可执行文件的输出重定向到文件。 然后,由于输出匹配属性文件格式,您可以读取该输出文件并使用其中的属性,例如:

    <target name="xtest" depends="xyz" description="Additional check">
        <exec executable="${xtest.exe}" failonerror="true" resultproperty="retVal" output="output.txt">
            <arg value="${inputfile.dat}" />
        </exec>
        <echo>Returned: ${retVal}</echo>
        <property file="output.txt" prefix="xtestoutput"/>
        <fail>
            <condition>
                <not>
                    <equals arg1="${xtestoutput.Errors}" arg2="0" />
                </not>
            </condition>
        </fail>
    </target>
    

    【讨论】:

    • 谢谢@guleryuz。经过几次更改后工作。 1. 将 outputproperty 更改为 output 2. ` ` 为 &lt;condition&gt; &lt;not&gt; &lt;equals arg1="${xtestoutput.Errors}" arg2="0" /&gt; &lt;/not&gt; &lt;/condition&gt;
    【解决方案2】:

    与下面的代码完全一致。谢谢!

    <target name="xtest" depends="xyz" description="Additional check">
        <exec executable="${xtest.exe}" failonerror="true" resultproperty="retVal" output="output.txt">
            <arg value="${inputfile.dat}" />
        </exec>
        <echo>Returned: ${retVal}</echo>
        <property file="output.txt" prefix="xtestoutput"/>
        <fail>
            <condition>
              <not>
                <equals arg1="${xtestoutput.Errors}" arg2="0" />
              </not>
            </condition>
        </fail>
    </target>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多