How do I get status of tasks that gradle runs?

I am using a bash script on linux (RHEL 6.5) to invoke gradlew to run flyway commands. I have a sample script online at When the script runs, after gradlew performs its mojo, I use the shell exit status “$?” to determine whether the flyway commands worked or not. But gradlew seems to always succeed:

Output from ‘marssrv-marsdb,sh’ script is:

/gradlew --quiet -Pflyway.initOnMigrate=true -Pflyway.url=jdbc:mysql://localhost:3306/mars flywayRepair

FAILURE: Build failed with an exception.

What went wrong: Execution failed for task ‘:common:flywayRepair’.

Error occurred while executing flywayRepair Unable to obtain Jdbc connection from DataSource (jdbc:mysql://localhost:3306/mars) for user ‘flyaway’ Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. Connection refused

Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

Exit status from flywayRepair for mars is 0

The other script in github (‘db-repair’) is doing the same thing as the previous except it invokes a single flyway command then checks status. But no matter what, it still succeeds.

Already answered on Stack Overflow: