-->

停止ant脚本没有失败的构建(Stop ant script without failing bui

2019-08-05 08:13发布

在我的ant脚本我想退出(停止执行编译),而当条件满足失败。 我曾尝试使用:

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

Ant脚本停止条件,但构建失败。 我希望要停止的构建,但没有任何错误。 我使用“调用Ant”在詹金斯的一步。

谢谢。

Answer 1:

我会建议重新考虑你的方法重构你的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>


Answer 2:

使用内置的(因为JDK 6)JavaScript引擎,不需要额外的库(antcontrib ..等)需要!

1.爪哇System.exit()的

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

你可能会在Eclipse中运行时失败BUILD:构建失败javax.script.ScriptException:sun.org.mozilla.javascript.internal.WrappedException:裹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中,没有生成成功打印:

   [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

在包裹重用,铁一macrodef:

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


Answer 3:

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

个人而言,我不知道是否有任何其他的任务,以“正常”退出身材,而是自己写一个是不难的。

写它执行下面的代码应该工作任务...

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


文章来源: Stop ant script without failing build