Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 766
fei Avatar asked Sep 03 '09 18:09

fei


3 Answers

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.
like image 131
Renaud Avatar answered Sep 23 '22 06:09

Renaud


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.

like image 39
Rich Seller Avatar answered Sep 23 '22 06:09

Rich Seller


Correct solution for unix/linux:

mvn clean install
rc=$?
if [ $rc -ne 0 ] ; then
  echo Could not perform mvn clean install, exit code [$rc]; exit $rc
fi

The "if" statement itself is a command and if it is successful, it will reset the $? variable to 0. Same goes for echo. So, you have to use an intermediary local var, for example $rc to store the return code from "mvn clean install", then it can be passed to the "exit" command as well.

like image 39
PrecisionLex Avatar answered Sep 23 '22 06:09

PrecisionLex