检测Ant exec任务中的超时

时间:2012-09-19 19:54:08

标签: java android ant

在Ant exec任务中设置timeout属性并且任务超时时,是否有办法检测超时?我没有看到表示超时的结果,输出或错误属性中有任何用处。

1 个答案:

答案 0 :(得分:2)

<exec>由于超时而终止子进程时,父Ant进程会记录消息Timeout: killed the sub-process。但是,由于<exec>重定向器仅捕获子进程的输出,因此<exec> outputPropertyerrorProperty中没有指示超时。

要设置指示子进程超时的属性,可以使用<record>任务捕获Ant的日志输出,如以下示例所示。

<target name="exec-timeout">
  <record name="exec.log" action="start" />
    <exec executable="java" timeout="1500">
      <arg line="-jar /path/to/executable.jar" />
    </exec>
  <record name="exec.log" action="stop" />      

  <condition property="timed-out" else="false">
    <resourcecontains resource="exec.log"
        substring="Timeout: killed the sub-process" />
  </condition>
  <delete file="exec.log" />

  <echo message="exec timed out: ${timed-out}" />
</target>

输出

exec-timeout:
     [exec] Timeout: killed the sub-process
     [exec] Result: 143
     [echo] exec timed out: true

BUILD SUCCESSFUL
Total time: 2 seconds
相关问题