gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 14:52:58 24 4
gpt4 key购买 nike

我正在使用 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>

当出现错误时,构建不会失败。

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

发现回到构建目标,因此当调用失败时,该属性未设置。

那正确吗?

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

最佳答案

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

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

关于ant - 如果在两个文件中的任何一个中检测到错误,如何使 Ant 构建失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1505421/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com