在Ant exec任务中检测超时

Cli*_*iff 5 java ant android

当您在Ant exec任务中设置timeout属性并且该任务使进程超时时,是否可以检测到超时?我的结果,输出或错误属性没有任何有用的指示超时的信息。

Chr*_*ert 3

<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>
Run Code Online (Sandbox Code Playgroud)

输出

<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>
Run Code Online (Sandbox Code Playgroud)