如果在两个文件中检测到错误,如何使Ant构建失败?

时间:2009-10-01 17:46:54

标签: ant

我使用Ant来执行数据库构建,基本上使用exec任务来运行一些SQL脚本。

但是,在脚本执行期间可能会出现错误(例如,无法正确删除已连接的用户等),因此我通过查看两个输出日志文件来检查这一点。

以下是相关目标的片段:

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

如果有错误,构建不会失败。

我认为这是因为检查日志的 antcall 任务不会返回属性错误。

找回构建目标,因此当调用fail-if-error时,将取消设置该属性。

是吗?

有没有办法将其设置为正确失败?

3 个答案:

答案 0 :(得分:4)

antcall会将属性设置为执行范围,因此当您进行检查时,它不会被设置。而是尝试使用macrodef,这将在当前作用域中运行并在该作用域中设置errors-found属性,以便后面的检查可以读取它。你可以定义像这样的macrodef:

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

并将其称为:

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

答案 1 :(得分:3)

感谢Rich Seller,他提供了使用macrodef的想法。 macrodef需要一点清理(macrodef中不允许使用属性,任务需要包含在顺序标记中)所以我在这里完整地提供它:

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

答案 2 :(得分:0)

检查错误和警告的常规日志文件

这是一个常规的macrodef,可用于扫描问题的文件。只要您可以为该问题编写正则表达式,它就可以检查它......

  • 如果发现问题,可能会失败。
  • 它总结了发现的问题,将它们写入Ant输出。
  • 要扫描的文件可以用通配符表示。

以下是检查日志文件是否存在Oracle错误的示例调用:

  • “SP2-”错误失败
  • 警告“ORA-”错误
  • 警告“错误:”文字。

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

以下是在执行之前检查生成的sql文件中未替换的令牌的示例调用:

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

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