Friday, January 24, 2003

Ant; An Example of The Ant Condition Task

I needed to test the condition code returned by stopping the Apache service so that I could delete the log files. I settled on the following script:
  <property name="apache.service"    value="Apache" />
  <property name="sleep.in.seconds"  value="4" />

  <target name="shutdown_apache" description="stops the Apache service.">
    <exec executable="c:/WINNT/system32/net.exe" resultproperty="apache.stop.resultproperty" outputproperty="apache.stop.outputproperty">
      <arg value="stop" />
      <arg value="${apache.service}" />
    </exec>
    <condition property="apache.not.stopped">
      <equals arg1="${apache.stop.resultproperty}" arg2="2" />
    </condition>
    <antcall target="sleep_apache_stop" />
  </target>

  <target name="sleep_apache_stop" unless="apache.not.stopped">
    <echo>Sleeping for ${sleep.in.seconds} seconds.</echo>
    <sleep seconds="${sleep.in.seconds}" />
  </target>
Post a Comment