views:

909

answers:

3

i'm writing a bash script to automate the build process. there are two major build blocks, one is an ant task and one is a plain old "mvn clean install". i want to do something when there are build error coming from either of this two build processes.

and the problem is, these builds will contain test failures or errors from time to time, but the end result is successful. and i believe that the status code ($?) return by these processes should be 0 no matter the build fail or succeed, i could be wrong.

so what is the best way for my script to detect the end result (build fail/succeed) without catching the false info during the mid build (test errors, etc) from them?

+1  A: 

According to the ant manual:

the ant start up scripts (in their Windows and Unix version) return the return code of the java program. So a successful build returns 0, failed builds return other values.

Maven also returns a non-zero exit code on error. Here's a link showing how to interrogate this status using the Maven Invocation API.

So it seems to me that you should be able to explicitly handle the return codes in your script . Presumably you can ignore error codes relating to tests etc. if those are not a concern to you.

exec error codes in ant are operating system-specific. These may help you:

ire_and_curses
A: 

Here is exactly what I do to get the result you want.

    <exec executable="${env.M2_HOME}/bin/mvn" dir="${basedir}"
          failonerror="true" osfamily="unix">
        <arg value="-DskipTests=${argSkipTests}"/>
        <arg value="-Doffline=${argOffline}"/>
        <arg line="${projectsLine}"/>
        <arg line="${resumeFromLine}"/>
        <arg line="${alsoMakeLine}"/>
        <arg line="${alsoMakeDependentsLine}"/>
        <arg line="${commandsLine}"/>
    </exec>
joekutner
+2  A: 

There are a few issues against Maven 2 returning incorrect return codes (i.e. always returning 0). Notably MNG-3651 that was fixed in Maven 2.0.9.

In older versions, mvn.bat ended with this line:

exit /B %ERROR_CODE%

From Maven 2.0.9 onwards, the last line was changed to this:

cmd /C exit /B %ERROR_CODE%

So a non-0 return code is returned if the build fails. In the case of a build ERROR the return code is 1. If you are unable to upgrade to 2.0.9+, you could consider modifying mvn.bat as above to return the correct code.

Rich Seller