停止ant脚本而不失败生成

在我的ant脚本中,我希望退出(停止执行构建),而不会在满足条件时失败。 我曾尝试使用:

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

Ant脚本在条件下停止,但构建失败。 我想要构建停止但没有错误。 我在Jenkins使用“Invoke 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>

使用内建(自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:BUILD FAILED javax.script.ScriptException:sun.org.mozilla.javascript.internal.WrappedException:Wrapped org.eclipse.ant.internal.launching.remote.AntSecurityException =>然后使用Ant api代替

2.蚂蚁api

<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 BUILD SUCCESSFUL打印2次:

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

BUILD SUCCESSFUL
Total time: 0 seconds

BUILD SUCCESSFUL
Total time: 0 seconds

包装在macrodef中以供重用,fe:

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

内部<fail>抛出一个BuildException使构建过程失败,所以,只要你使用这个任务,你就会得到带有错误的ant构建出口。

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

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

public void execute() {
    // do something as you like
    System.exit(0);
}
链接地址: http://www.djcxy.com/p/44329.html

上一篇: Stop ant script without failing build

下一篇: Terminating a Python script