2009-10-01 20 views
6

Estoy utilizando Ant para hacer una compilación de base de datos utilizando básicamente la tarea exec para ejecutar algunas secuencias de comandos SQL.¿Cómo fallar Ant build si se detectan errores en cualquiera de los dos archivos?

Sin embargo, puede haber errores generados durante la ejecución de la secuencia de comandos (por ejemplo, no se pudo colocar correctamente a un usuario conectado, etc.), así que lo comprobo mirando a través de dos archivos de registro de salida.

He aquí un fragmento del objetivo relevante:

<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--> 
    <antcall target="check-log-file"> 
     <param name="file.to.check" value="${output.log.1}"/> 
    </antcall> 

    <antcall target="check-log-file"> 
     <param name="file.to.check" value="${output.log.2}"/> 
    </antcall> 

    <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. 
--> 
<target name="check-log-file" 
     description="Checks the file (specified in ${file.to.check}) for errors"> 
    <property name="file.errorcount" value="${file.to.check}.errorcount" 
       description="The file to hold the error lines"/> 
    <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"/> 
</target> 

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

Cuando hay errores de la acumulación no falla.

Creo que es porque la tarea anticaída para verificar los registros no devuelve el error de propiedad.

Encontrado de nuevo en el destino de compilación, por lo que cuando se invoca fail-if-error, esa propiedad no está configurada.

¿Es correcto?

¿Hay alguna manera de configurarlo para que no funcione correctamente?

Respuesta

3

Gracias a Rich vendedor, que proporcionó la idea de utilizar un macrodef. El macrodef necesitaba un poco de limpieza (propiedad no está permitido dentro de un macrodef, las tareas tienen que ser envuelto en una etiqueta secuencial) así que estoy proporcionando aquí en su totalidad:

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

Antcall establecerá la propiedad en el ámbito de su ejecución, por lo que cuando llegue a su cuenta no está configurada. En su lugar, intente utilizar macrodef, esto se ejecutará en el ámbito actual y establecerá la propiedad de errores encontrados en ese ámbito para que la verificación posterior pueda leerlo. Se podría definir la macrodef algo como esto:

<macrodef name="check-log-file"> 
    <attribute name="fileToCheck"/> 

    <!--note attributes are referenced with an "@" rather than a "$" --> 
    <property name="file.errorcount" value="@{fileToCheck}.errorcount"/> 
    <copy file="@{fileToCheck}" tofile="${file.errorcount}"> 

    ... 

</macrodef> 

y lo llaman así:

<check-log-file fileToCheck="${output.log.1}"/> 
<check-log-file fileToCheck="${output.log.1}"/> 
0

archivo de registro general de comprobar con errores y advertencias

Aquí hay un macrodef general que se puede usar para escanear archivos en busca de problemas. Siempre que pueda escribir una expresión regular para el problema, puede verificarla ...

  • Puede fallar o no si se detecta el problema.
  • Resume los problemas encontrados y los escribe en la salida Ant.
  • Los archivos a escanear se pueden indicar con comodines.

Éstos son ejemplos de llamadas para comprobar los archivos de registro de errores de Oracle:

  • fallar en errores "sp2"
  • advierten sobre los errores "ORA"
  • advierten sobre el "ERROR: "texto".

    <check_for_errors file.to.check.dir="${buildlogs}" file.to.check.include="*.log" error.pattern="SP2-" /> 
    <check_for_errors file.to.check.dir="${buildlogs}" file.to.check.include="*.log" error.pattern="ORA-" error.action="warn" /> 
    <check_for_errors file.to.check.dir="${buildlogs}" file.to.check.include="*.log" error.pattern="ERROR:" error.action="warn" /> 
    

Éstos son ejemplo llama a revisar los indicadores no reemplazados en los archivos SQL generado, antes de la ejecución:

<check_for_errors file.to.check.dir="${distdir}" file.to.check.include="**/\*.sql" 
     error.name="Token" error.pattern="^(?!--)[email protected][^@ ][email protected]" error.display.find=".*(@[^@ ][email protected]).*" error.display.show=" Token = '\1'"/> 
    <check_for_errors file.to.check.dir="${distdir}" file.to.check.include="**/*.sql" 
     error.name="Token" error.pattern="^(?!--)[email protected]\$\{[^ }]+\}" error.display.find=".*(\$\{[^ }]+\}).*" error.display.show=" Token = '\1'"/> 

Aquí es el macrodef:

<macrodef name="check_for_errors"> 
    <attribute name="file.to.check.dir" default="." /> 
    <attribute name="file.to.check.include" default="*.log" /> 
    <attribute name="file.to.check.exclude" default="" /> 
    <attribute name="error.pattern" default="ERROR" /> 
    <attribute name="error.name" default="ERROR" /> 
    <attribute name="error.action" default="fail" /> 
    <attribute name="error.display.find" default="(.+)" /> 
    <attribute name="error.display.show" default=" \1" /> 
    <sequential> 
     <echo message="Excluding file ${buildlogfile}" level="debug" /> 
     <for param="file.to.check.name"> 
      <fileset dir="@{file.to.check.dir}"> 
       <include name="@{file.to.check.include}"/> 
       <exclude name="@{file.to.check.exclude}"/> 
       <exclude name="${buildlogfile}"/> 
       <containsregexp expression="@{error.pattern}"/> 
      </fileset> 
      <sequential> 
       <echo message="ERROR: @{error.name} found in file '@{file.to.check.name}' :" level="warn" /> 
       <concat> 
        <fileset file="@{file.to.check.name}" /> 
        <filterchain> 
         <linecontainsregexp> 
          <regexp pattern="@{error.pattern}" /> 
         </linecontainsregexp> 
         <replaceregex pattern="@{error.display.find}" replace="@{error.display.show}" /> 
        </filterchain> 
       </concat> 
       <property name="error.check.foundvalues" value="true" /> 
      </sequential> 
     </for> 
     <condition property="error.check.fail"> 
      <and> 
       <matches string="@{error.action}" pattern="fail" /> 
       <isset property="error.check.foundvalues" /> 
      </and> 
     </condition> 
     <fail message="ERROR: Fix the above errors and try again. Exiting..." if="error.check.fail"/>  
    </sequential> 
</macrodef> 
Cuestiones relacionadas