Stop ant script without crashing

In my ant script, I want to exit (stop the build) without crashing when the condition is met. I tried using:

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

The Ant script stops on condition, but the build failed. I want the assembly to be stopped, but without errors. I am using "Invoke Ant" in Jenkins.

Thanks.

+7
source share
3 answers

I would suggest reorganizing your ant script by redefining your approach. If you approach your problem with “execution of the assembly when a certain condition is met” instead of “failure of the assembly if another condition is met”, it is easier to implement it:

 <!-- 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> 
+9
source

Using the built-in (with JDK 6) javascript mechanism, no additional libraries are needed (antcontrib .. etc.)!

1. Java System.exit ()

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

you can get BUILD FAILED when working in Eclipse: STRICTLY FAULT javax.script.ScriptException: sun.org.mozilla.javascript.internal.WrappedException: Wrapped org.eclipse.ant.internal.launching.remote.AntSecurityException => then use Ant api instead

2. Ant api

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

Eclipse output, does not print BUILD SUCCESSFUL:

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

The output console, BUILD SUCCESSFUL is printed 2 times:

  [script] Ending intentionally [script] ... [script] .. [script] . BUILD SUCCESSFUL Total time: 0 seconds BUILD SUCCESSFUL Total time: 0 seconds 

Wrap the macrodef for reuse, i.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> 

Examples

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

output:

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


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

output:

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

Internally, <fail> throws a BuildException to disable the build process, so while you use this task, you will get the ant build with errors.

Personally, I don’t know if there is any other task to exit the assembly normally, but writing it yourself is not difficult.

Writing a task that executes the following code should work ...

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

All Articles