how to detect a build error from ant/maven via a bash script?

fei picture fei · Sep 3, 2009 · Viewed 34.3k times · Source

I am 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 is build error coming from either of this two build processes.

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?

Answer

Renaud picture Renaud · May 7, 2014
mvn clean test
if [[ "$?" -ne 0 ]] ; then
  echo 'could not perform tests'; exit $rc
fi
  • $? is a special shell variable that contains the exit code (whether it terminated successfully, or not) of the most immediate recently executed command.
  • -ne stands for "not equal". So here we are testing if the exit code from mvn clean is not equal to zero.