【问题标题】:Stop ant script without failing build停止 ant 脚本而不失败构建
【发布时间】:2012-12-17 07:23:34
【问题描述】:

在我的 ant 脚本中,我想在满足条件时退出(停止执行构建)而不失败。我试过用:

<if>
    <equals arg1="${variable1}" arg2="${variable2}" />
  <then>
    <fail status="0" message="No change, exit" />
  </then>
</if>

Ant 脚本在条件下停止但构建失败。我想停止构建但没有错误。我在 Jenkins 中使用“Invoke Ant”步骤。

谢谢。

【问题讨论】:

  • 为什么一个名为 fail 的任务会做任何事情而不是失败?

标签: ant build ant-contrib


【解决方案1】:

使用内置(自 JDK 6 起)javascript 引擎, 不需要额外的库(antcontrib .. 等)!

1. Java System.exit()

<script language="javascript">
 self.log("Ending intentionally \n...\n..\n.");
 java.lang.System.exit(0);
</script> 

在 Eclipse 中运行时可能会出现 BUILD FAILED : 构建失败 javax.script.ScriptException:sun.org.mozilla.javascript.internal.WrappedException: 包装好的 org.eclipse.ant.internal.launching.remote.AntSecurityException => 然后改用 Ant api

2.蚂蚁接口

<script language="javascript">
 self.log("Ending intentionally \n...\n..\n.");
 project.fireBuildFinished(null);
</script>

输出 Eclipse,没有 BUILD SUCCESSFUL 被打印出来:

   [script] Ending intentionally 
   [script] ...
   [script] ..
   [script] .
Total time: 410 milliseconds

输出控制台,BUILD SUCCESSFUL 打印 2 次:

   [script] Ending intentionally
   [script] ...
   [script] ..
   [script] .

BUILD SUCCESSFUL
Total time: 0 seconds

BUILD SUCCESSFUL
Total time: 0 seconds

包装在一个宏定义中以供重用,f.e. :

<macrodef name="stopbuild">
 <attribute name="condition"/>
 <attribute name="paramtype" default="math"/>
 <attribute name="param1"/>
 <attribute name="param2"/>
 <attribute name="when" default="true"/>
 <sequential>
 <script language="javascript">
 falsecondition = false;
 switch ("@{condition}")
 {
  case "lt" :
   b = "@{paramtype}" == "math" ? parseInt("@{param1}") &lt; parseInt("@{param2}") : "@{param1}" &lt; "@{param2}";
   break;
  case "gt" :
   b = "@{paramtype}" == "math" ? parseInt("@{param1}") &gt; parseInt("@{param2}") : "@{param1}" &gt; "@{param2}";
   break;
  case "eq" :
   b = "@{paramtype}" == "math" ? parseInt("@{param1}") == parseInt("@{param2}") : "@{param1}" == "@{param2}";
   break;
  default:
   self.log("Wrong condition : @{condition}, supported: lt, gt, eq");
   falsecondition = true;
 }
 if(!falsecondition &amp;&amp; b == java.lang.Boolean.valueOf("@{when}")) {
   self.log("Stopping Build because @{param1} @{condition} @{param2} is @{when} !!");
   java.lang.System.exit(0);
   // alternatively use
   //project.fireBuildFinished(null);
 }
 </script>
 </sequential>
</macrodef>

例子

 <!-- compare int, default paramtype=math and when=true -->
 <stopbuild param1="10" param2="11" condition="lt"/>

输出:

[script] Stopping Build because 10 lt 11 is true !!


 <!-- compare strings, default when=true -->
 <stopbuild param1="abc" param2="abcd" paramtype="foo" condition="lt"/>

输出:

[script] Stopping Build because abc lt abcd is true !!

【讨论】:

  • IMO,这是一个有趣的解决方案,但比公认的答案复杂得多。
  • 并不复杂,其实是最简单的。不用放在宏定义中 - 这只是为了演示。毕竟它不需要外部库,只需使用内置的 javascript 引擎。
  • 这种方法有一个突出的缺点。这是提出的第二种方法。这个挺重的。
【解决方案2】:

我建议通过重新考虑您的方法来重构您的 ant 脚本。如果您通过“在满足某个条件时执行构建”而不是“如果满足另一个条件则使构建失败”来解决您的问题,那么实现起来会更容易:

<!-- add on top of your build file -->
<if>
    <equals arg1="${variable1}" arg2="${variable2}" />
  <then>
    <property name="runBuild" value="true"/>
  </then>
  <else>
    <property name="runBuild" value="false"/>
  </else>
</if>


<!-- add to the target that shall be executed conditionally -->
<target name="myTarget" if="${runBuild}">
...

<!-- exit message as separate target -->
<target name="exitTarget" unless="${runBuild}">
  <echo message="No Change, exit" />
</target>

【讨论】:

    【解决方案3】:

    在内部&lt;fail&gt; 会抛出BuildException 以使构建过程失败,因此,只要您使用此任务,您就会得到带有错误的ant 构建退出。

    我个人不知道是否还有其他任务可以“正常”退出构建,但是自己写一个并不难。

    编写执行以下代码的任务应该可以工作...

    public void execute() {
        // do something as you like
        System.exit(0);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-27
      • 1970-01-01
      • 1970-01-01
      • 2019-09-20
      • 2019-03-04
      • 2016-05-23
      • 1970-01-01
      相关资源
      最近更新 更多