maven.el
changeset 1508 fad1a57bf25f
parent 1302 82d6e8bd0861
child 1510 96a2dcb07960
equal deleted inserted replaced
1507:6f939639c52a 1508:fad1a57bf25f
       
     1 ;;; maven.el --- Maven build project helpers.
       
     2 
       
     3 ;;; Commentary:
       
     4 ;;
       
     5 
       
     6 ;;; Code:
       
     7 
       
     8 (defvar maven.command "mvn")
       
     9 
       
    10 (defvar maven.help-buffer-name "*Maven Help*")
       
    11 
       
    12 (defun maven.parse-pom-dependency-in-region (start end)
       
    13   (let (groupId artifactId version)
       
    14     (save-restriction
       
    15       (narrow-to-region start end)
       
    16       (goto-char start)
       
    17       (re-search-forward "<groupId>\\([^<]*\\)</groupId>" nil t)
       
    18       (setq groupId (match-string-no-properties 1))
       
    19       (goto-char start)
       
    20       (re-search-forward "<artifactId>\\([^<]*\\)</artifactId>" nil t)
       
    21       (setq artifactId (match-string-no-properties 1))
       
    22       (goto-char start)
       
    23       (re-search-forward "<version>\\([^<]*\\)</version>" nil t)
       
    24       (setq version (match-string-no-properties 1))
       
    25       (list groupId artifactId version)
       
    26       )))
       
    27 
       
    28 (defun maven.parse-pom-dependency (&optional point)
       
    29   (unless point
       
    30     (setq point (point)))
       
    31   (let (start end pos groupId artifactId version fs-o bs-o fs-c bs-c)
       
    32     (save-excursion
       
    33       (goto-char point)
       
    34       (unless (eq (char-after) ?<)
       
    35         (search-backward "<"))
       
    36       (setq point (point))
       
    37       (setq fs-o (re-search-forward "<\\(?:plugin\\|dependency\\)>" nil t))
       
    38       (when fs-o
       
    39         (setq fs-o (- fs-o 12)))
       
    40       (goto-char point)
       
    41       (setq bs-o (re-search-backward "<\\(?:plugin\\|dependency\\)>" nil t))
       
    42       (goto-char point)
       
    43       (setq fs-c (re-search-forward "</\\(?:plugin\\|dependency\\)>" nil t))
       
    44       (goto-char point)
       
    45       (setq bs-c (re-search-backward "</\\(?:plugin\\|dependency\\)>" nil t))
       
    46       (when bs-c
       
    47         (setq bs-c (+ bs-c 13)))
       
    48       (cond
       
    49        ((and fs-o (= fs-o point) fs-c)
       
    50         (maven.parse-pom-dependency-in-region fs-o fs-c))
       
    51        ((and bs-o (<= bs-o point) fs-c (<= point fs-c))
       
    52         (maven.parse-pom-dependency-in-region bs-o fs-c))
       
    53        ((and fs-o fs-c)
       
    54         (maven.parse-pom-dependency-in-region fs-o fs-c))
       
    55        ((and bs-o bs-c)
       
    56         (maven.parse-pom-dependency-in-region bs-o bs-c))
       
    57        (t
       
    58         (list nil nil nil)))
       
    59       )))
       
    60 ;; (pp (maven.parse-pom-dependency))
       
    61 
       
    62 ;;;###autoload
       
    63 (defun maven.help ()
       
    64   "Run help:describe for plugin at point."
       
    65   (interactive)
       
    66   (let (dependency groupId artifactId)
       
    67     (setq dependency (maven.parse-pom-dependency))
       
    68     (setq groupId (elt dependency 0))
       
    69     (setq artifactId (elt dependency 1))
       
    70     (if (not (and groupId artifactId))
       
    71         (message "Can't find `groupId' or `artifactId'")
       
    72       (shell-command
       
    73        (format "%s help:describe -Ddetail -DgroupId=%s -DartifactId=%s" maven.command groupId artifactId)
       
    74        (switch-to-buffer maven.help-buffer-name)) )
       
    75     ))
       
    76 
       
    77 ;;;###autoload
       
    78 (defun maven.effective-pom ()
       
    79   "Run help:effective-pom for plugin at point."
       
    80   (interactive)
       
    81   (shell-command
       
    82    (format "%s help:effective-pom" maven.command)
       
    83    (switch-to-buffer maven.help-buffer-name)) )
       
    84 
       
    85 ;;;###autoload
       
    86 (defun maven.effective-settings ()
       
    87   "Run help:effective-settings for plugin at point."
       
    88   (interactive)
       
    89   (shell-command
       
    90    (format "%s help:effective-settings" maven.command)
       
    91    (switch-to-buffer maven.help-buffer-name)) )
       
    92 
       
    93 ;;;###autoload
       
    94 (defun maven.dependency-tree ()
       
    95   "Run dependency:tree for plugin at point."
       
    96   (interactive)
       
    97   (shell-command
       
    98    (format "%s dependency:tree" maven.command)
       
    99    (switch-to-buffer maven.help-buffer-name)) )
       
   100 
       
   101 (defun maven.project-root ()
       
   102   "Root of project."
       
   103   (let ( (dir default-directory) (found nil) )
       
   104     (while (and (not found) (> (length dir) 5))
       
   105       (when (file-exists-p (concat dir "/pom.xml"))
       
   106         (setq found dir))
       
   107       (setq dir (expand-file-name (concat dir "/.."))))
       
   108     found))
       
   109 
       
   110 (defun maven.file-package ()
       
   111   "Return file package."
       
   112   (save-excursion
       
   113     (goto-char (point-min))
       
   114     (let (pkg cls)
       
   115       (re-search-forward "package +\\([[:alnum:]_.]+\\) *;" nil t)
       
   116       (setq pkg (match-string-no-properties 1))
       
   117       (re-search-forward "class\\s +\\([[:alnum:]_]+\\)\\(\\s \\|\n\\|implements [^{]*\\|extents [^{]*\\)*{" nil t)
       
   118       (setq cls (match-string-no-properties 1))
       
   119       (when (and pkg cls)
       
   120         (concat pkg "." cls))) ))
       
   121 
       
   122 ;;;###autoload
       
   123 (defun maven.run-file ()
       
   124   "Run exec:java for current file."
       
   125   (interactive)
       
   126   (let* ( (default-directory (concat (maven.project-root) "/")) )
       
   127     (if (not default-directory)
       
   128         (message "Can't find maven project root")
       
   129       (compilation-start
       
   130        (format "%s exec:java -Dexec.mainClass=%s" maven.command (maven.file-package))))))
       
   131 
       
   132 ;;;###autoload
       
   133 (defun maven.run-test ()
       
   134   "Run test -Dtest=... for current file."
       
   135   (interactive)
       
   136   (let* ( (default-directory (concat (maven.project-root) "/")) )
       
   137     (if (not default-directory)
       
   138         (message "Can't find maven project root")
       
   139       (compilation-start
       
   140        (format "%s test -Dtest=%s" maven.command (maven.file-package))))))
       
   141 
       
   142 (provide 'maven)
       
   143 
       
   144 ;;; maven.el ends here