When using ant or phing to run tests in build.xml file, you can catch exceptions by using trycatch block as shown below.


Example


If the composer task under try block fails any given reason, we catch the error/exception within catch block. After catching it, we remove the content of vendor folder then run composer again. This process happens only once. Note: I am deliberatelly causing an exception with fail block for demonstration purposes.


<?xml version="1.0" encoding="UTF-8"?>
<project name="inanzzz" default="build" basedir=".">

<target name="build" depends="composer"/>

<target name="composer">
<trycatch>
<try>
<echo>Running composer install first time ...</echo>
<exec executable="composer" passthru="true" checkreturn="true">
<arg value="install"/>
</exec>
<autoloader autoloaderpath="vendor/autoload.php"/>

<fail>Throw exception on purpose</fail>
</try>

<catch>
<echo>Removing vendors ...</echo>
<delete includeemptydirs="true">
<fileset dir="vendor">
<include name="**/*" />
</fileset>
</delete>

<echo>Running composer install again ...</echo>
<exec executable="composer" passthru="true" checkreturn="true">
<arg value="install"/>
</exec>
<autoloader autoloaderpath="vendor/autoload.php"/>
</catch>
</trycatch>
</target>

</project>