Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ant conditional failure upon executable failure

Tags:

exec

ant

I have the following ant target:

<target name="refactor-ids">
    <echo>Refactor IDs</echo>
    <exec executable="perl" dir="${basedir}">
        <arg value="script.pl" />
        <arg value="input.xml" />

    </exec> 
</target>

If the executable fails for any reason (script.pl doesnt exist, etc) the build will resolve as successful. How do I establish conditional build success upon the success of this executable?

like image 550
JD. Avatar asked Oct 17 '11 21:10

JD.


3 Answers

just add failonerror="true" to the exec element

like image 178
yonix Avatar answered Nov 03 '22 02:11

yonix


I am assumng this is an ant script and not an xsl target.

You can use the attribute failifexecutionfails of the exec task :

http://ant.apache.org/manual/Tasks/exec.html

So if your execution fails for any reason your build will also fail. This is by default true. You can also check for the return code of your executable by using attributes :

failonerror

and

resultproperty

e.g.

<target name="refactor-ids">
    <echo>Refactor IDs</echo>
    <exec executable="perl" dir="${basedir}" failonerror="false" resultproperty="return.code">
        <arg value="script.pl" />
        <arg value="input.xml" />

    </exec> 
    <fail>
     <condition>
       <equals arg1="-1" arg2="${return.code}"/>
     </condition>
   </fail>
</target>
like image 20
FailedDev Avatar answered Nov 03 '22 02:11

FailedDev


To fail immediately, use the failonerror attribute:

<exec dir="${basedir}" executable="sh" failonerror="true">
    <arg line="-c 'myscript'" />
</exec>

To perform some other action before failing, store the exit code in the resultproperty attribute. Typically, 0 indicates success, and 1 or higher indicates an error:

<exec dir="${basedir}" executable="sh" failonerror="false" resultproperty="exitStatusCode">
    <arg line="-c 'myscript'" />
</exec>

<!-- Do some other stuff before failing -->

<fail>
    <condition>
        <not>
            <equals arg1="0" arg2="${exitStatusCode}"/>
        </not>
    </condition>
</fail>
like image 42
scarswell Avatar answered Nov 03 '22 02:11

scarswell