@jpkotta's answer is fine for me, except that when I only have a single buffer, it keeps the opened compilation buffer. I check for this case and close the window:
(defun bury-compile-buffer-if-successful (buffer string)
"Bury a compilation buffer if succeeded without warnings."
(when (and
(buffer-live-p buffer)
(string-match "compilation" (buffer-name buffer))
(string-match "finished" string)
)
(run-with-timer 1 nil
(lambda (buf)
(bury-buffer buf)
(switch-to-prev-buffer (get-buffer-window buf) 'kill)
;; delete window if it was opened by the compilation process
;; (have two windows with the same buffer)
(when
(and (equal 2 (length (window-list)))
(eq (window-buffer (car (window-list))) (window-buffer (cadr (window-list)))))
(delete-window (selected-window))
)
)
buffer)))
(add-hook 'compilation-finish-functions 'bury-compile-buffer-if-successful)