Ant - Error handling

on Saturday, 5 May 2012
I found this on the net a long time ago and thought I would share. Basically, it parses a log file and extracts the errors.

 <target name="build">
  
   <echo message="Add foo bar baz"/>
  
        <exec executable="${db.sqlplus}">
  
   </exec>
  
   <echo message="Load x y z"/>
  
        <exec executable="${db.sqlplus}" dir="foobar">
  
   </exec>
  
   <!--Check the log files here-->
  
           <check-log-file fileToCheck="${output.log.1}"/>
  
           <check-log-file fileToCheck="${output.log.2}"/>
  
   <antcall target="fail-if-error"/>
  
 </target>
  
 <!--=================================================================================
  
   Check the file named in the property file.to.check to see if there are errors.
  
   The way this works is to find all lines containing the text "ERROR" and put
  
   them into a separate file. Then it checks to see if this file has non-zero
  
   length. If so, then there are errors, and it sets the property errors.found.
  
   Then it calls the send-email target, which doesn't execute if the errors.found
  
   property isn't set.
  
 -->
  
 <macrodef name="check-log-file">
  
   <attribute name="file.to.check"/>
  
   <attribute name="file.errorcount" default="@{file.to.check}.errorcount" description="The file to hold the error lines"/>
  
   <sequential>
  
     <copy file="@{file.to.check}" tofile="@{file.errorcount}">
  
       <filterchain>
  
         <linecontains>
  
           <contains value="ERROR"/>
  
         </linecontains>
  
       </filterchain>
  
     </copy>
  
     <condition property="errors.found" value="true">
  
       <length file="@{file.errorcount}" when="gt" length="0"/>
  
     </condition>
  
     <antcall target="check-log-file-send-email">
  
       <param name="file.to.check"  value="@{file.to.check}"/>
  
     </antcall>
  
   </sequential>
  
 </macrodef>
  
 <!--=================================================================================
  
   If there are any errors, send an email to let someone know
  
 -->
  
 <target name="check-log-file-send-email" if="errors.found" description="Sends an email out if error detected">
  
   <resourcecount property="error.count">
  
     <tokens><!-- default tokenizer is a line tokenizer -->
  
       <file file="${file.to.check}.errorcount"/>
  
     </tokens>
  
   </resourcecount>
  
   <echo message="Database build (${e1.codeline} - ${error.count} errors found..."/>
  
   <antcall target="mail">
  
     <param name="from-address" value="build"/>
  
     <param name="to-list"    value="myemail"/>
  
     <param name="subject"    value="Automated database build error report for ${db.host}"/>
  
     <param name="message"    value="See attached log file, ${error.count} error(s) found..."/>
  
     <param name="attach"    value="${file.to.check}"/>
  
   </antcall>
  
 </target>
  
 <!--=================================================================================
  
   Fails the database build if errors were detected.
  
 -->
  
 <target name="fail-if-error" if="errors.found">
  
   <echo message="Errors found - setting database fail flag..."/>
  
   <fail message="Errors detected during ${codeline} database build. Check logs."/>
  
 </target>  

0 comments:

Post a Comment