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