5

当你在 Ant exec 任务中设置了 timeout 属性,并且任务超时了进程,有没有办法检测到 timeout 呢?我在结果、输出或错误属性中没有看到任何有用的指示超时的信息。

4

1 回答 1

3

When <exec> kills a subprocess due to a timeout, the parent Ant process logs the message Timeout: killed the sub-process. However, since the <exec> redirector only captures output from the subprocess, there is no indication of the timeout in the <exec> outputProperty or errorProperty.

To set a property indicating the subprocess timed out, Ant's log output can be captured using the <record> task as demonstrated in the following example.

<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>

Output

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

BUILD SUCCESSFUL
Total time: 2 seconds
于 2012-09-19T20:27:35.527 回答