Search code examples
antbuildant-contrib

Stop ant script without failing build


In my ant script I want to exit (stop executing build) without failing when a condition is met. I have tried to use:

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

Ant script is stopped on condition but build is failed. I want to the build to be stopped but with no errors. I'm using "Invoke Ant" step in Jenkins.

Thanks.


Solution

  • I would suggest to refactor your ant script by reconsidering your approach. If you approach your problem with "execution of a build when a certain condition is met" instead of "failing the build if another condition is met" it is easier to implement:

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