Run exec:java for current file.
authorOleksandr Gavenko <gavenkoa@gmail.com>
Wed, 09 Jul 2014 23:18:24 +0300
changeset 1144 7b3c20a4ddfa
parent 1143 a7fb97ff9cb6
child 1145 ce218b01999d
Run exec:java for current file.
maven-central.el
--- a/maven-central.el	Wed Jun 25 17:26:14 2014 +0300
+++ b/maven-central.el	Wed Jul 09 23:18:24 2014 +0300
@@ -169,6 +169,36 @@
    (format "%s dependency:tree" maven.command)
    (switch-to-buffer maven.help-buffer-name)) )
 
+(defun maven.project-root ()
+  "Root of project."
+  (let ( (dir default-directory) (found nil) )
+    (while (and (not found) (> (length dir) 5))
+      (when (file-exists-p (concat dir "/pom.xml"))
+        (setq found dir))
+      (setq dir (expand-file-name (concat dir "/.."))))
+    found))
+
+(defun maven.file-package ()
+  "Return file package."
+  (save-excursion
+    (goto-char (point-min))
+    (let (pkg cls)
+      (re-search-forward "package +\\([[:alnum:]_.]+\\) *;" nil t)
+      (setq pkg (match-string-no-properties 1))
+      (re-search-forward "class +\\([[:alnum:]_]+\\)\s *" nil t)
+      (setq cls (match-string-no-properties 1))
+      (when (and pkg cls)
+        (concat pkg "." cls))) ))
+
+(defun maven.run-file ()
+  "Run exec:java for current file."
+  (interactive)
+  (let* ( (default-directory (concat (maven.project-root) "/")) )
+    (if (not default-directory)
+        (message "Can't find maven project root")
+      (compilation-start
+       (format "%s exec:java -Dexec.mainClass=%s" maven.command (maven.file-package))))))
+
 ;; (maven-central.last-version "junit" "junit")
 
 (provide 'maven-central)