It works great!
The previous code fails if COMMAND starts with a relative directory.
This version is more robust in that regard:
(let ((default-directory (file-name-as-directory dir)))
(defun compilation-compile-with-current-line (dir)
"Extract the current line as a command and execute it in DIR."
(interactive "DExecute command in directory: ")
(let ((command (substring-no-properties (thing-at-point 'line) 0 -1)))
;; Do it this way instead of using "cd DIR; COMMAND"
;; to avoid outrageous prefixing on repeated invocations.
(compile command))))