maven.el
changeset 1666 06937ff1ec5f
parent 1665 3685e2321a9b
child 1667 7f70095fbf32
equal deleted inserted replaced
1665:3685e2321a9b 1666:06937ff1ec5f
     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   (let (start end pos groupId artifactId version fs-o bs-o fs-c bs-c)
       
    30     (save-excursion
       
    31       (if (looking-at "[^>]*<[^!]")
       
    32           (progn (search-forward "<") (backward-char))
       
    33         (search-backward "<"))
       
    34       (setq point (point))
       
    35       (when (re-search-forward "<\\(?:plugin\\|dependency\\)>" nil t)
       
    36         (setq fs-o (re-search-backward "<" nil t)))
       
    37       (goto-char point)
       
    38       (setq bs-o (re-search-backward "<\\(?:plugin\\|dependency\\)>" nil t))
       
    39       (goto-char point)
       
    40       (setq fs-c (re-search-forward "</\\(?:plugin\\|dependency\\)>" nil t))
       
    41       (goto-char point)
       
    42       (when (re-search-backward "</\\(?:plugin\\|dependency\\)>" nil t)
       
    43         (setq bs-c (re-search-forward ">" nil t)))
       
    44       (cond
       
    45        ((and fs-o (= fs-o point) fs-c)
       
    46         (maven/parse-pom-dependency-in-region fs-o fs-c))
       
    47        ((and bs-o (<= bs-o point) fs-c (<= point fs-c))
       
    48         (maven/parse-pom-dependency-in-region bs-o fs-c))
       
    49        ((and fs-o fs-c)
       
    50         (maven/parse-pom-dependency-in-region fs-o fs-c))
       
    51        ((and bs-o bs-c)
       
    52         (maven/parse-pom-dependency-in-region bs-o bs-c))
       
    53        (t
       
    54         (list nil nil nil)))
       
    55       )))
       
    56 ;; (pp (maven/parse-pom-dependency))
       
    57 
       
    58 ;;;###autoload
       
    59 (defun maven-help ()
       
    60   "Run help:describe for plugin at point."
       
    61   (interactive)
       
    62   (let (dependency groupId artifactId)
       
    63     (setq dependency (maven/parse-pom-dependency))
       
    64     (setq groupId (elt dependency 0))
       
    65     (setq artifactId (elt dependency 1))
       
    66     (if (not (and groupId artifactId))
       
    67         (message "Can't find `groupId' or `artifactId'")
       
    68       (shell-command
       
    69        (format "%s help:describe -Ddetail -DgroupId=%s -DartifactId=%s" maven-command groupId artifactId)
       
    70        (switch-to-buffer maven/help-buffer-name)) )
       
    71     ))
       
    72 
       
    73 ;;;###autoload
       
    74 (defun maven-effective-pom ()
       
    75   "Run help:effective-pom for plugin at point."
       
    76   (interactive)
       
    77   (shell-command
       
    78    (format "%s help:effective-pom" maven-command)
       
    79    (switch-to-buffer maven/help-buffer-name)) )
       
    80 
       
    81 ;;;###autoload
       
    82 (defun maven-effective-settings ()
       
    83   "Run help:effective-settings for plugin at point."
       
    84   (interactive)
       
    85   (shell-command
       
    86    (format "%s help:effective-settings" maven-command)
       
    87    (switch-to-buffer maven/help-buffer-name)) )
       
    88 
       
    89 ;;;###autoload
       
    90 (defun maven-dependency-tree ()
       
    91   "Run dependency:tree for plugin at point."
       
    92   (interactive)
       
    93   (shell-command
       
    94    (format "%s dependency:tree" maven-command)
       
    95    (switch-to-buffer maven/help-buffer-name)) )
       
    96 
       
    97 (defun maven/project-root ()
       
    98   "Root of project."
       
    99   (let ( (dir default-directory) (found nil) )
       
   100     (while (and (not found) (> (length dir) 5))
       
   101       (when (file-exists-p (concat dir "/pom.xml"))
       
   102         (setq found dir))
       
   103       (setq dir (expand-file-name (concat dir "/.."))))
       
   104     found))
       
   105 
       
   106 (defun maven/file-package ()
       
   107   "Return file package."
       
   108   (save-excursion
       
   109     (goto-char (point-min))
       
   110     (let (pkg cls)
       
   111       (re-search-forward "package +\\([[:alnum:]_.]+\\) *;" nil t)
       
   112       (setq pkg (match-string-no-properties 1))
       
   113       (re-search-forward "class\\s +\\([[:alnum:]_]+\\)\\(\\s \\|\n\\|implements [^{]*\\|extents [^{]*\\)*{" nil t)
       
   114       (setq cls (match-string-no-properties 1))
       
   115       (when (and pkg cls)
       
   116         (concat pkg "." cls))) ))
       
   117 
       
   118 ;;;###autoload
       
   119 (defun maven-run-file ()
       
   120   "Run exec:java for current file."
       
   121   (interactive)
       
   122   (let* ( (default-directory (concat (maven/project-root) "/")) )
       
   123     (if (not default-directory)
       
   124         (message "Can't find maven project root")
       
   125       (compilation-start
       
   126        (format "%s exec:java -Dexec.mainClass=%s" maven-command (maven/file-package))))))
       
   127 
       
   128 ;;;###autoload
       
   129 (defun maven-run-test ()
       
   130   "Run test -Dtest=... for current file."
       
   131   (interactive)
       
   132   (let* ( (default-directory (concat (maven/project-root) "/")) )
       
   133     (if (not default-directory)
       
   134         (message "Can't find maven project root")
       
   135       (compilation-start
       
   136        (format "%s test -Dtest=%s" maven-command (maven/file-package))))))
       
   137 
       
   138 (provide 'maven)
       
   139 
       
   140 ;;; maven.el ends here