|
| 1 | +-- Maven command runner with UI |
| 2 | +local M = {} |
| 3 | + |
| 4 | +-- Store last command for quick re-run |
| 5 | +M.last_command = nil |
| 6 | + |
| 7 | +-- Common Maven phases and goals |
| 8 | +M.maven_commands = { |
| 9 | + -- Lifecycle Phases |
| 10 | + { name = "clean", desc = "Clean the project (remove target/)" }, |
| 11 | + { name = "validate", desc = "Validate project structure" }, |
| 12 | + { name = "compile", desc = "Compile source code" }, |
| 13 | + { name = "test", desc = "Run unit tests" }, |
| 14 | + { name = "test-compile", desc = "Compile test sources" }, |
| 15 | + { name = "package", desc = "Package compiled code (JAR/WAR)" }, |
| 16 | + { name = "verify", desc = "Run integration tests" }, |
| 17 | + { name = "install", desc = "Install package to local repository" }, |
| 18 | + { name = "deploy", desc = "Deploy to remote repository" }, |
| 19 | + |
| 20 | + -- Common Combined Commands |
| 21 | + { name = "clean compile", desc = "Clean and compile" }, |
| 22 | + { name = "clean test", desc = "Clean and test" }, |
| 23 | + { name = "clean package", desc = "Clean and package" }, |
| 24 | + { name = "clean install", desc = "Clean and install" }, |
| 25 | + |
| 26 | + -- Plugin Goals |
| 27 | + { name = "dependency:tree", desc = "Display dependency tree" }, |
| 28 | + { name = "dependency:analyze", desc = "Analyze dependencies" }, |
| 29 | + { name = "versions:display-dependency-updates", desc = "Check for dependency updates" }, |
| 30 | + { name = "help:effective-pom", desc = "Show effective POM" }, |
| 31 | + |
| 32 | + -- Spring Boot specific (if applicable) |
| 33 | + { name = "spring-boot:run", desc = "Run Spring Boot application" }, |
| 34 | + |
| 35 | + -- Testing specific |
| 36 | + { name = "test -Dtest=", desc = "Run specific test class (specify after =)", custom_input = true }, |
| 37 | + { name = "test -Dtest=*Test", desc = "Run all test classes" }, |
| 38 | + { name = "surefire-report:report", desc = "Generate test report" }, |
| 39 | +} |
| 40 | + |
| 41 | +-- Execute Maven command |
| 42 | +function M.execute_maven(cmd, opts) |
| 43 | + opts = opts or {} |
| 44 | + |
| 45 | + -- Check if mvn exists |
| 46 | + local mvn_cmd = vim.fn.executable("mvn") == 1 and "mvn" or nil |
| 47 | + if not mvn_cmd then |
| 48 | + vim.notify("Maven (mvn) not found in PATH. Install with: brew install maven", vim.log.levels.ERROR) |
| 49 | + return |
| 50 | + end |
| 51 | + |
| 52 | + -- Store last command |
| 53 | + M.last_command = cmd |
| 54 | + |
| 55 | + -- Determine if we're in a Maven project |
| 56 | + local pom_exists = vim.fn.filereadable(vim.fn.getcwd() .. "/pom.xml") == 1 |
| 57 | + if not pom_exists then |
| 58 | + vim.notify("No pom.xml found in current directory: " .. vim.fn.getcwd(), vim.log.levels.WARN) |
| 59 | + local proceed = vim.fn.confirm("Run Maven anyway?", "&Yes\n&No", 2) |
| 60 | + if proceed ~= 1 then |
| 61 | + return |
| 62 | + end |
| 63 | + end |
| 64 | + |
| 65 | + local full_cmd = "mvn " .. cmd |
| 66 | + |
| 67 | + -- Choose display method based on preference |
| 68 | + if opts.background then |
| 69 | + -- Run in background, show in quickfix |
| 70 | + vim.notify("Running: " .. full_cmd, vim.log.levels.INFO) |
| 71 | + vim.fn.setqflist({}, 'r', { title = full_cmd, lines = {} }) |
| 72 | + vim.cmd("copen") |
| 73 | + |
| 74 | + vim.fn.jobstart(full_cmd, { |
| 75 | + on_stdout = function(_, data) |
| 76 | + if data then |
| 77 | + vim.fn.setqflist({}, 'a', { lines = data }) |
| 78 | + end |
| 79 | + end, |
| 80 | + on_stderr = function(_, data) |
| 81 | + if data then |
| 82 | + vim.fn.setqflist({}, 'a', { lines = data }) |
| 83 | + end |
| 84 | + end, |
| 85 | + on_exit = function(_, exit_code) |
| 86 | + if exit_code == 0 then |
| 87 | + vim.notify("Maven command completed successfully", vim.log.levels.INFO) |
| 88 | + else |
| 89 | + vim.notify("Maven command failed with exit code: " .. exit_code, vim.log.levels.ERROR) |
| 90 | + end |
| 91 | + end, |
| 92 | + }) |
| 93 | + else |
| 94 | + -- Run in terminal split |
| 95 | + vim.cmd("botright 15split | terminal " .. full_cmd) |
| 96 | + vim.cmd("startinsert") |
| 97 | + end |
| 98 | +end |
| 99 | + |
| 100 | +-- Show Maven menu using vim.ui.select |
| 101 | +function M.show_menu() |
| 102 | + local choices = {} |
| 103 | + for i, cmd in ipairs(M.maven_commands) do |
| 104 | + choices[i] = string.format("%-40s | %s", cmd.name, cmd.desc) |
| 105 | + end |
| 106 | + |
| 107 | + vim.ui.select(choices, { |
| 108 | + prompt = "Select Maven command:", |
| 109 | + format_item = function(item) |
| 110 | + return item |
| 111 | + end, |
| 112 | + }, function(choice, idx) |
| 113 | + if not choice then |
| 114 | + return |
| 115 | + end |
| 116 | + |
| 117 | + local selected = M.maven_commands[idx] |
| 118 | + |
| 119 | + -- Handle custom input commands |
| 120 | + if selected.custom_input then |
| 121 | + vim.ui.input({ |
| 122 | + prompt = "Enter Maven command: ", |
| 123 | + default = selected.name, |
| 124 | + }, function(input) |
| 125 | + if input and input ~= "" then |
| 126 | + M.execute_maven(input) |
| 127 | + end |
| 128 | + end) |
| 129 | + else |
| 130 | + M.execute_maven(selected.name) |
| 131 | + end |
| 132 | + end) |
| 133 | +end |
| 134 | + |
| 135 | +-- Run last Maven command |
| 136 | +function M.run_last() |
| 137 | + if M.last_command then |
| 138 | + vim.notify("Re-running: mvn " .. M.last_command, vim.log.levels.INFO) |
| 139 | + M.execute_maven(M.last_command) |
| 140 | + else |
| 141 | + vim.notify("No previous Maven command to run", vim.log.levels.WARN) |
| 142 | + M.show_menu() |
| 143 | + end |
| 144 | +end |
| 145 | + |
| 146 | +-- Quick command to run specific test |
| 147 | +function M.run_test(test_name) |
| 148 | + if test_name then |
| 149 | + M.execute_maven("test -Dtest=" .. test_name) |
| 150 | + else |
| 151 | + -- Try to infer test name from current file |
| 152 | + local current_file = vim.fn.expand("%:t:r") -- filename without extension |
| 153 | + if current_file:match("Test$") or current_file:match("Tests$") then |
| 154 | + vim.ui.input({ |
| 155 | + prompt = "Test class name:", |
| 156 | + default = current_file, |
| 157 | + }, function(input) |
| 158 | + if input and input ~= "" then |
| 159 | + M.execute_maven("test -Dtest=" .. input) |
| 160 | + end |
| 161 | + end) |
| 162 | + else |
| 163 | + M.execute_maven("test") |
| 164 | + end |
| 165 | + end |
| 166 | +end |
| 167 | + |
| 168 | +-- User command to run Maven |
| 169 | +vim.api.nvim_create_user_command("Maven", function(opts) |
| 170 | + if opts.args and opts.args ~= "" then |
| 171 | + M.execute_maven(opts.args) |
| 172 | + else |
| 173 | + M.show_menu() |
| 174 | + end |
| 175 | +end, { |
| 176 | + nargs = "*", |
| 177 | + desc = "Run Maven command", |
| 178 | + complete = function() |
| 179 | + local completions = {} |
| 180 | + for _, cmd in ipairs(M.maven_commands) do |
| 181 | + table.insert(completions, cmd.name) |
| 182 | + end |
| 183 | + return completions |
| 184 | + end, |
| 185 | +}) |
| 186 | + |
| 187 | +-- User command to run tests |
| 188 | +vim.api.nvim_create_user_command("MavenTest", function(opts) |
| 189 | + M.run_test(opts.args ~= "" and opts.args or nil) |
| 190 | +end, { |
| 191 | + nargs = "?", |
| 192 | + desc = "Run Maven tests", |
| 193 | +}) |
| 194 | + |
| 195 | +return M |
0 commit comments