【问题标题】:Ant-contrib should fail but doesn'tAnt-contrib 应该失败但不会
【发布时间】:2016-03-08 16:42:41
【问题描述】:

我正在尝试使用一个包含两个目标的脚本来部署多个系统。当一个系统部署失败时,我想停止整个脚本并失败。

<target name="deploy_all">
    <for list="${systems}" param="system" delimiter="," parallel="false" threadCount="1" trim="true">
        <sequential>
            <antcall target="deploy_one_system">
                <param name="system" value="@{system}" />
            </antcall>
        </sequential>
    </for>
</target>

<target name="deploy_one_system">
    <trycatch property="error_system">
        <try>
            <!-- deployment -->
            <!-- Deep in other targets, there's <fail> -->
        </try>
        <catch>
            <echo>Error during deployment of ${system}:</echo>
            <echo>${error_system}</echo>
            <!-- print logs, errors, cleanup -->
            <if>
                <contains string="${stop_execution_on_fail}" substring="${system}" />
                <then>
                    <echo message="I should fail here!" />
                    <fail message="Error occured during deploying ${system}."/>
                </then>
            </if>
        </catch>
    </trycatch>
</target>

问题是条件被正确评估并且消息“我应该在这里失败!”被打印,但构建没有失败并继续部署下一个系统。

变量 ${stop_execution_on_fail} 被提供给脚本并包含应该使整个构建失败(而不是部署其余系统)的系统列表。

有时,在部署多个内存不足的系统后构建会失败。

17:07:03 deploy.xml:900: 
17:07:03 The following error occurred while executing this line:
17:07:03 deploy.xml:908: The following error occurred while executing this line:
17:07:03 deploy.xml:802: Error occured during deploying system1.
17:07:03 The following error occurred while executing this line:
17:07:03 deploy.xml:908: The following error occurred while executing this line:
17:07:03 deploy.xml:802: Error occured during deploying system2.
17:07:03 The following error occurred while executing this line:
17:07:03 deploy.xml:908: The following error occurred while executing this line:
17:07:03 deploy.xml:802: Error occured during deploying system3.
17:07:03 The following error occurred while executing this line:
17:07:03 deploy.xml:908: The following error occurred while executing this line:
17:07:03 deploy.xml:4: java.lang.OutOfMemoryError: PermGen space
17:07:03 The following error occurred while executing this line:
17:07:03 deploy.xml:908: The following error occurred while executing this line:
17:07:03 deploy.xml:4: java.lang.OutOfMemoryError: PermGen space

我正在运行 Jenkins 1.642.1、JDK 1.8.0_74 和 Ant 1.9.2。

有什么想法吗?

编辑(基于 pczeus 的评论):打印以下内容(不要介意时间戳,我从另一个版本中获取):

10:12:56      [echo] Error during deployment of system1:
10:12:56      [echo] The following error occurred while executing this line:
10:12:56      [echo] deploy.xml:739: The following error occurred while executing this line:
10:12:56      [echo] deploy.xml:647: The following error occurred while executing this line:
10:12:56      [echo] deploy.xml:473: The following error occurred while executing this line:
10:12:56      [echo] dbmaintain.xml:229: Unable to perform db maintain task.

--- omitted ---

10:12:56      [echo] I should fail here!

如您所见,条件评估成功,因为消息 I should fail here! 被打印出来。

stop_execution_on_fail 变量包含以逗号分隔的失败系统列表:

system1,system2,system3

【问题讨论】:

  • 修复 PermGen 空间问题:stackoverflow.com/questions/6387380/…
  • @BrunoLavit 谢谢,当我测试使用 JDK6 运行脚本时,我把它留在了错误消息中。 JDK8 不打印任何东西,它只是被卡住了。
  • 尝试使用-logger org.apache.tools.ant.listener.ProfileLogger 选项运行Ant。这将显示 Ant 是否在 &lt;echo&gt; 任务之后进入了 &lt;fail&gt; 任务。还可以考虑使用 -debug 选项运行 Ant,看看它是否揭示了任何问题。
  • 谢谢,对我帮助很大!我不知道 ˛-logger 开关。

标签: jenkins ant ant-contrib


【解决方案1】:

我相信你的问题是在你的

&lt;contains string="${stop_execution_on_fail}" substring="${system}" /&gt;

您正在检查整个字符串stop_execution_on_fail 中与系统匹配的子字符串。但是,您的尝试:

<trycatch property="error_system">

正在 error_system 属性中设置错误消息,而您没有将其签入包含。

尝试将&lt;contains&gt; 更改为:

<contains string="${error_system}" substring="${system}" />

【讨论】:

  • 您好,感谢您的建议,但这不是问题所在。我在问题的脚本摘录中添加了相应的部分,所以现在不应该混淆。正确评估条件并打印 块中的回显消息。
  • 您能告诉我们 2 个&lt;echo&gt; 语句为 system 和 erorr_system 打印的内容吗?您还可以为 stop_execution_on_fail 添加回声并提供吗?唯一有意义的是比较失败了。
  • 我将其添加到问题中。
  • 其实我以为条件是根本原因,但是加上了“我应该在这里失败”的回声就被推翻了。
【解决方案2】:

我使用Chad Nouis' 的建议追踪了错误并发现了以下内容:

  • 首先,当我没有发布实际代码而只是摘录并替换了一些变量时,我很愚蠢。真丢脸!
  • &lt;for&gt; 调用中的parallel 属性在deploy_all 目标中设置为真。在这种情况下,即使将 threadCount 设置为 1,Ant 也会使目标失败,但不会阻止 for 循环运行下一个循环(尽管我确信它应该这样做)。

谢谢Chad Nouis

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-21
    • 1970-01-01
    • 2018-07-24
    • 2011-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多