Skip to content

Commit 045644d

Browse files
authored
Merge pull request #416 from artur-shaik/ant
Ant
2 parents 84bb14e + db4041a commit 045644d

File tree

3 files changed

+188
-1
lines changed

3 files changed

+188
-1
lines changed
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
let s:antXmlTemplate = [
2+
\ ' <target name="vjc-test-conditions">',
3+
\ ' <condition property="vjc-netbeans-condition">',
4+
\ ' <isset property="javac.classpath" />',
5+
\ ' </condition>',
6+
\ ' <condition property="vjc-project-condition">',
7+
\ ' <isreference refid="project.classpath"/>',
8+
\ ' </condition>',
9+
\ ' <condition property="vjc-classpath-condition">',
10+
\ ' <isreference refid="classpath"/>',
11+
\ ' </condition>',
12+
\ ' </target>',
13+
\ ' <target name="vjc-netbeans-classpath" depends="vjc-test-conditions" if="vjc-netbeans-condition">',
14+
\ ' <property name="javavi.classpath" value="${javac.classpath}" />',
15+
\ ' </target>',
16+
\ ' <target name="vjc-project-classpath" depends="vjc-test-conditions" if="vjc-project-condition">',
17+
\ ' <property name="javavi.classpath" refid="project.classpath"/>',
18+
\ ' </target>',
19+
\ ' <target name="vjc-classpath" depends="vjc-test-conditions" if="vjc-classpath-condition">',
20+
\ ' <property name="javavi.classpath" refid="project.classpath"/>',
21+
\ ' </target>',
22+
\ ' <target name="vjc-printclasspath" depends="vjc-project-classpath,vjc-netbeans-classpath,vjc-classpath">',
23+
\ ' <echo message="${javavi.classpath}"/>',
24+
\ ' </target>']
25+
26+
function! s:Log(log)
27+
let log = type(a:log) == type("") ? a:log : string(a:log)
28+
call javacomplete#logger#Log("[classpath.ant] ". log)
29+
endfunction
30+
31+
function! javacomplete#classpath#ant#IfAnt()
32+
if executable('ant') && g:JavaComplete_AntPath != ""
33+
return 1
34+
endif
35+
return 0
36+
endfunction
37+
38+
function! javacomplete#classpath#ant#Generate(force) abort
39+
let g:JavaComplete_ProjectKey = substitute(g:JavaComplete_AntPath, '[\\/:;.]', '_', 'g')
40+
let path = javacomplete#util#GetBase("classpath". g:FILE_SEP). g:JavaComplete_ProjectKey
41+
42+
if filereadable(path)
43+
if a:force == 0 && getftime(path) >= getftime(g:JavaComplete_AntPath)
44+
call s:Log("get libs from cache file")
45+
return join(readfile(path), '')
46+
endif
47+
call javacomplete#util#RemoveFile(javacomplete#util#GetBase('cache'). g:FILE_SEP. 'class_packages_'. g:JavaComplete_ProjectKey. '.dat')
48+
endif
49+
50+
let s:antPath = path
51+
let s:antOutput = []
52+
let cmd = "ant -projecthelp -v | grep '^ init\\>'"
53+
call javacomplete#util#RunSystem(
54+
\ cmd, "ant check 'init' target process",
55+
\ "javacomplete#classpath#ant#CheckInitTargetHandler")
56+
return '.'
57+
endfunction
58+
59+
function! javacomplete#classpath#ant#CheckInitTargetHandler(data, event)
60+
if a:event == 'exit'
61+
if a:data == "0"
62+
let hasInitTarget = !empty(s:antOutput)
63+
let s:antOutput = []
64+
call s:BuildAntClasspath(hasInitTarget)
65+
else
66+
echohl WarningMsg | echomsg "Failed to check 'init' target" | echohl None
67+
endif
68+
elseif a:event == 'stdout'
69+
for data in filter(a:data,'v:val =~ "^ init\\>.*$"')
70+
if g:JavaComplete_ShowExternalCommandsOutput
71+
echomsg data
72+
endif
73+
if exists('s:antOutput')
74+
call add(s:antOutput, data)
75+
endif
76+
endfor
77+
elseif a:event == 'stderr'
78+
for data in filter(a:data,'v:val !~ "^\\s*$"')
79+
echoerr data
80+
endfor
81+
endif
82+
endfunction
83+
84+
function! s:BuildAntClasspath(hasInitTarget)
85+
let tmpBuildFile = []
86+
for line in readfile(g:JavaComplete_AntPath)
87+
if stridx(line, '</project>') >= 0
88+
if a:hasInitTarget
89+
let xmlTemplate = s:antXmlTemplate
90+
let xmlTemplate[0] = xmlTemplate[0][:-2]. ' depends="init">'
91+
call extend(tmpBuildFile, xmlTemplate)
92+
else
93+
call extend(tmpBuildFile, s:antXmlTemplate)
94+
endif
95+
endif
96+
call add(tmpBuildFile, line)
97+
endfor
98+
let s:tmpAntFileName = "vjc-ant-build.xml"
99+
call writefile(tmpBuildFile, s:tmpAntFileName)
100+
101+
let s:antOutput = []
102+
let antCmd = ['ant', '-f', s:tmpAntFileName, '-q', 'vjc-printclasspath']
103+
call javacomplete#util#RunSystem(
104+
\ antCmd, "ant classpath build process",
105+
\ "javacomplete#classpath#ant#BuildClasspathHandler")
106+
endfunction
107+
108+
function! javacomplete#classpath#ant#BuildClasspathHandler(data, event)
109+
if a:event == 'exit'
110+
if a:data == "0"
111+
for line in s:antOutput
112+
let matches = matchlist(line, '\m^\s\+\[echo\]\s\+\(.*\)')
113+
if !empty(matches)
114+
let cp = matches[1]
115+
break
116+
endif
117+
endfor
118+
if cp != '.'
119+
call writefile([cp], s:antPath)
120+
endif
121+
122+
let g:JavaComplete_LibsPath .= ':'. cp
123+
124+
call javacomplete#util#RemoveFile(javacomplete#util#GetBase('cache'). g:FILE_SEP. 'class_packages_'. g:JavaComplete_ProjectKey. '.dat')
125+
126+
call javacomplete#server#UnblockStart()
127+
call javacomplete#server#Terminate()
128+
call javacomplete#server#Start()
129+
130+
echomsg "Ant classpath built successfully"
131+
else
132+
echohl WarningMsg | echomsg "Failed to build ant classpath" | echohl None
133+
endif
134+
135+
call delete(s:tmpAntFileName)
136+
137+
unlet s:antOutput
138+
unlet s:tmpAntFileName
139+
140+
elseif a:event == 'stdout'
141+
for data in filter(a:data,'v:val !~ "^\\s*$"')
142+
if g:JavaComplete_ShowExternalCommandsOutput
143+
echomsg data
144+
endif
145+
endfor
146+
if exists('s:antOutput')
147+
call extend(s:antOutput, a:data)
148+
endif
149+
elseif a:event == 'stderr'
150+
for data in filter(a:data,'v:val !~ "^\\s*$"')
151+
echoerr data
152+
endfor
153+
endif
154+
endfunction
155+
" vim:set fdm=marker sw=2 nowrap:

autoload/javacomplete/classpath/classpath.vim

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
function! s:Log(log)
2+
let log = type(a:log) == type("") ? a:log : string(a:log)
3+
call javacomplete#logger#Log("[classpath] ". log)
4+
endfunction
5+
16
function! javacomplete#classpath#classpath#BuildClassPath()
27
call s:BuildClassPath(0)
38
endfunction
@@ -12,6 +17,7 @@ function! s:BuildClassPath(force)
1217
let g:JavaComplete_PomPath = javacomplete#util#FindFile('pom.xml')
1318
if g:JavaComplete_PomPath != ""
1419
let g:JavaComplete_PomPath = fnamemodify(g:JavaComplete_PomPath, ':p')
20+
call s:Log("found maven file: ". g:JavaComplete_PomPath)
1521
endif
1622
endif
1723
endif
@@ -25,11 +31,28 @@ function! s:BuildClassPath(force)
2531
endif
2632
if g:JavaComplete_GradlePath != ""
2733
let g:JavaComplete_GradlePath = fnamemodify(g:JavaComplete_GradlePath, ':p')
34+
call s:Log("found gradle file: ". g:JavaComplete_GradlePath)
35+
endif
36+
endif
37+
endif
38+
39+
if !get(g:, 'JavaComplete_AntRepositoryDisabled', 0)
40+
if !exists('g:JavaComplete_AntPath')
41+
if filereadable(getcwd(). g:FILE_SEP. "build.xml")
42+
let g:JavaComplete_AntPath = getcwd(). g:FILE_SEP. "build.xml"
43+
else
44+
let g:JavaComplete_AntPath = javacomplete#util#FindFile('build.xml', '**3')
45+
endif
46+
if g:JavaComplete_AntPath != ""
47+
let g:JavaComplete_AntPath = fnamemodify(g:JavaComplete_AntPath, ':p')
48+
call s:Log("found ant file: ". g:JavaComplete_AntPath)
2849
endif
2950
endif
3051
endif
3152

3253
let g:JavaComplete_LibsPath .= s:FindClassPath(a:force)
54+
55+
call s:Log("libs found: ". g:JavaComplete_LibsPath)
3356
endfunction
3457

3558
function! s:ReadClassPathFile(classpathFile)
@@ -68,12 +91,21 @@ function! s:UseGradle(force)
6891
return ""
6992
endf
7093

94+
function! s:UseAnt(force)
95+
if javacomplete#classpath#ant#IfAnt()
96+
return javacomplete#classpath#ant#Generate(a:force)
97+
endif
98+
99+
return ""
100+
endf
101+
71102
function! s:FindClassPath(force) abort
72103
for classpathSourceType in g:JavaComplete_ClasspathGenerationOrder
73104
try
74105
let cp = ''
75106
exec "let cp .= s:Use". classpathSourceType. "(". a:force. ")"
76107
if !empty(cp)
108+
call s:Log("found ". classpathSourceType. " project")
77109
return '.' . g:PATH_SEP . cp
78110
endif
79111
catch

plugin/javacomplete.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ let g:JavaComplete_ShowExternalCommandsOutput =
3030
\ get(g:, 'JavaComplete_ShowExternalCommandsOutput', 0)
3131

3232
let g:JavaComplete_ClasspathGenerationOrder =
33-
\ get(g:, 'g:JavaComplete_ClasspathGenerationOrder', ['Eclipse', 'Maven', 'Gradle'])
33+
\ get(g:, 'g:JavaComplete_ClasspathGenerationOrder', ['Eclipse', 'Maven', 'Gradle', 'Ant'])
3434

3535
let g:JavaComplete_ImportSortType =
3636
\ get(g:, 'JavaComplete_ImportSortType', 'jarName')

0 commit comments

Comments
 (0)