-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathmaven.lua
More file actions
130 lines (122 loc) · 3.72 KB
/
maven.lua
File metadata and controls
130 lines (122 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
local utils = require("kide.tools")
local M = {
mvn = vim.fn.exepath("mvn"),
}
local function maven_settings()
if vim.fn.filereadable(vim.fn.expand("~/.m2/settings.xml")) == 1 then
return vim.fn.expand("~/.m2/settings.xml")
end
local maven_home = vim.env["MAVEN_HOME"]
if maven_home then
local settings_xml = vim.fs.joinpath(maven_home, "conf", "settings.xml")
if vim.fn.filereadable(settings_xml) == 1 then
return settings_xml
end
end
end
M.get_maven_settings = function()
return vim.env["MAVEN_SETTINGS_XML"] or maven_settings()
end
M.is_pom_file = function(file)
return vim.endswith(file, "pom.xml")
end
local exec = function(cmd, args)
local opt = vim.tbl_deep_extend("force", cmd, {})
local s = M.get_maven_settings()
if s then
table.insert(opt, "-s")
table.insert(opt, s)
end
local p = vim.fn.expand("%")
if M.is_pom_file(p) then
table.insert(opt, "-f")
table.insert(opt, p)
end
if args and vim.trim(args) ~= "" then
vim.list_extend(opt, vim.split(args, " "))
end
require("kide.term").toggle(opt)
end
local function create_command(buf, name, cmd, complete)
vim.api.nvim_buf_create_user_command(buf, name, function(opts)
if type(cmd) == "function" then
cmd = cmd(opts)
end
if cmd == nil then
return
end
exec(cmd, opts.args)
end, {
nargs = "*",
complete = complete,
})
end
local maven_args_complete = utils.command_args_complete
M.maven_command = function(buf)
-- 判断为 java 文件
if vim.api.nvim_get_option_value("filetype", { buf = buf }) == "java" then
create_command(buf, "MavenExecJava", function(_)
local filename = vim.fn.expand("%:p")
filename = string.gsub(filename, "^[%-/%w%s]*%/src%/main%/java%/", "")
filename = string.gsub(filename, "[/\\]", ".")
filename = string.gsub(filename, "%.java$", "")
return { "mvn", 'exec:java -Dexec.mainClass="' .. filename .. '"' }
end, nil)
end
create_command(
buf,
"MavenCompile",
{ "mvn", "clean", "compile" },
maven_args_complete({ "test-compile" }, { model = "multiple" })
)
create_command(
buf,
"MavenInstall",
{ "mvn", "clean", "install" },
maven_args_complete({ "-DskipTests", "-Dmaven.test.skip=true" }, { model = "single" })
)
create_command(
buf,
"MavenPackage",
{ "mvn", "clean", "package" },
maven_args_complete({ "-DskipTests", "-Dmaven.test.skip=true" }, { model = "single" })
)
create_command(
buf,
"MavenDependencyTree",
{ "mvn", "dependency:tree" },
maven_args_complete({ "-Doutput=.dependency.txt" }, { model = "single" })
)
create_command(buf, "MavenDependencyAnalyzeDuplicate", { "mvn", "dependency:analyze-duplicate" }, nil)
create_command(buf, "MavenDependencyAnalyzeOnly", { "mvn", "dependency:analyze-only", "-Dverbose" }, nil)
create_command(buf, "MavenDownloadSources", { "mvn", "dependency:sources", "-DdownloadSources=true" })
create_command(buf, "MavenTest", { "mvn", "test" }, maven_args_complete({ "-Dtest=" }, { model = "single" }))
end
M.setup = function()
local group = vim.api.nvim_create_augroup("kide_jdtls_java_maven", { clear = true })
vim.api.nvim_create_autocmd({ "FileType" }, {
group = group,
pattern = { "xml", "java" },
desc = "maven_command",
callback = function(e)
if vim.endswith(e.file, "pom.xml") or vim.endswith(e.file, ".java") then
M.maven_command(e.buf)
end
end,
})
vim.api.nvim_create_user_command("Maven", function(opts)
exec({ "mvn" }, opts.args)
end, {
nargs = "*",
complete = maven_args_complete({
"clean",
"compile",
"test-compile",
"verify",
"package",
"install",
"deploy",
}, { model = "multiple" }),
})
end
return M