/** Exits the build immediately with a given exit code. */
  public void exit(int code) {
    if (buildEventListener != null) {
      buildEventListener.triggerEvent("Exiting", code);
    }

    // Prevent system.exit during unit/integration testing
    if (System.getProperty("grails.cli.testing") != null
        || System.getProperty("grails.disable.exit") != null) {
      throw new ScriptExitException(code);
    }
    GrailsConsole.getInstance().flush();
    System.exit(code);
  }