diff -r 3685e2321a9b -r 06937ff1ec5f mylisp/maven.el --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mylisp/maven.el Sat Jan 02 00:33:04 2021 +0200 @@ -0,0 +1,140 @@ +;;; maven.el --- Maven build project helpers. + +;;; Commentary: +;; + +;;; Code: + +(defvar maven-command "mvn") + +(defvar maven/help-buffer-name "*Maven Help*") + +(defun maven/parse-pom-dependency-in-region (start end) + (let (groupId artifactId version) + (save-restriction + (narrow-to-region start end) + (goto-char start) + (re-search-forward "\\([^<]*\\)" nil t) + (setq groupId (match-string-no-properties 1)) + (goto-char start) + (re-search-forward "\\([^<]*\\)" nil t) + (setq artifactId (match-string-no-properties 1)) + (goto-char start) + (re-search-forward "\\([^<]*\\)" nil t) + (setq version (match-string-no-properties 1)) + (list groupId artifactId version) + ))) + +(defun maven/parse-pom-dependency (&optional point) + (let (start end pos groupId artifactId version fs-o bs-o fs-c bs-c) + (save-excursion + (if (looking-at "[^>]*<[^!]") + (progn (search-forward "<") (backward-char)) + (search-backward "<")) + (setq point (point)) + (when (re-search-forward "<\\(?:plugin\\|dependency\\)>" nil t) + (setq fs-o (re-search-backward "<" nil t))) + (goto-char point) + (setq bs-o (re-search-backward "<\\(?:plugin\\|dependency\\)>" nil t)) + (goto-char point) + (setq fs-c (re-search-forward "" nil t)) + (goto-char point) + (when (re-search-backward "" nil t) + (setq bs-c (re-search-forward ">" nil t))) + (cond + ((and fs-o (= fs-o point) fs-c) + (maven/parse-pom-dependency-in-region fs-o fs-c)) + ((and bs-o (<= bs-o point) fs-c (<= point fs-c)) + (maven/parse-pom-dependency-in-region bs-o fs-c)) + ((and fs-o fs-c) + (maven/parse-pom-dependency-in-region fs-o fs-c)) + ((and bs-o bs-c) + (maven/parse-pom-dependency-in-region bs-o bs-c)) + (t + (list nil nil nil))) + ))) +;; (pp (maven/parse-pom-dependency)) + +;;;###autoload +(defun maven-help () + "Run help:describe for plugin at point." + (interactive) + (let (dependency groupId artifactId) + (setq dependency (maven/parse-pom-dependency)) + (setq groupId (elt dependency 0)) + (setq artifactId (elt dependency 1)) + (if (not (and groupId artifactId)) + (message "Can't find `groupId' or `artifactId'") + (shell-command + (format "%s help:describe -Ddetail -DgroupId=%s -DartifactId=%s" maven-command groupId artifactId) + (switch-to-buffer maven/help-buffer-name)) ) + )) + +;;;###autoload +(defun maven-effective-pom () + "Run help:effective-pom for plugin at point." + (interactive) + (shell-command + (format "%s help:effective-pom" maven-command) + (switch-to-buffer maven/help-buffer-name)) ) + +;;;###autoload +(defun maven-effective-settings () + "Run help:effective-settings for plugin at point." + (interactive) + (shell-command + (format "%s help:effective-settings" maven-command) + (switch-to-buffer maven/help-buffer-name)) ) + +;;;###autoload +(defun maven-dependency-tree () + "Run dependency:tree for plugin at point." + (interactive) + (shell-command + (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\\s +\\([[:alnum:]_]+\\)\\(\\s \\|\n\\|implements [^{]*\\|extents [^{]*\\)*{" nil t) + (setq cls (match-string-no-properties 1)) + (when (and pkg cls) + (concat pkg "." cls))) )) + +;;;###autoload +(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)))))) + +;;;###autoload +(defun maven-run-test () + "Run test -Dtest=... 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 test -Dtest=%s" maven-command (maven/file-package)))))) + +(provide 'maven) + +;;; maven.el ends here