Skip to content

Commit ad4d38a

Browse files
committed
initial ant support
extending `build.xml` file with target that print classpath. trying to guess classpath variable. related #391
1 parent 84bb14e commit ad4d38a

File tree

3 files changed

+107
-1
lines changed

3 files changed

+107
-1
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
\ ' </target>',
10+
\ ' <target name="vjc-netbeans-classpath" depends="vjc-test-conditions" if="vjc-netbeans-condition">',
11+
\ ' <property name="javavi.classpath" value="${javac.classpath}" />',
12+
\ ' </target>',
13+
\ ' <target name="vjc-project-classpath" depends="vjc-test-conditions" if="vjc-project-condition">',
14+
\ ' <property name="javavi.classpath" refid="project.classpath"/>',
15+
\ ' </target>',
16+
\ ' <target name="vjc-printclasspath" depends="vjc-project-classpath,vjc-netbeans-classpath">',
17+
\ ' <echo message="${javavi.classpath}"/>',
18+
\ ' </target>']
19+
20+
function! s:Log(log)
21+
let log = type(a:log) == type("") ? a:log : string(a:log)
22+
call javacomplete#logger#Log("[classpath.ant] ". log)
23+
endfunction
24+
25+
function! javacomplete#classpath#ant#IfAnt()
26+
if executable('ant') && g:JavaComplete_AntPath != ""
27+
return 1
28+
endif
29+
return 0
30+
endfunction
31+
32+
function! javacomplete#classpath#ant#Generate(force) abort
33+
let g:JavaComplete_ProjectKey = substitute(g:JavaComplete_AntPath, '[\\/:;.]', '_', 'g')
34+
let path = javacomplete#util#GetBase("classpath". g:FILE_SEP). g:JavaComplete_ProjectKey
35+
36+
call s:Log(path)
37+
if filereadable(path)
38+
if a:force == 0 && getftime(path) >= getftime(g:JavaComplete_AntPath)
39+
call s:Log("get libs from cache file")
40+
return join(readfile(path), '')
41+
endif
42+
call javacomplete#util#RemoveFile(javacomplete#util#GetBase('cache'). g:FILE_SEP. 'class_packages_'. g:JavaComplete_ProjectKey. '.dat')
43+
endif
44+
45+
let hasInitTarget = !empty(system("ant -projecthelp -v | grep '^ init\\>'"))
46+
let tmpFile = []
47+
for line in readfile(g:JavaComplete_AntPath)
48+
if stridx(line, '</project>') >= 0
49+
if hasInitTarget
50+
let xmlTemplate = s:antXmlTemplate
51+
let xmlTemplate[0] = xmlTemplate[0][:-2]. ' depends="init">'
52+
call extend(tmpFile, xmlTemplate)
53+
else
54+
call extend(tmpFile, s:antXmlTemplate)
55+
endif
56+
endif
57+
call add(tmpFile, line)
58+
endfor
59+
let s:temporaryAntFile = "vjc-ant-build.xml"
60+
call writefile(tmpFile, s:temporaryAntFile)
61+
62+
let antCmd = ['ant', '-f', s:temporaryAntFile, '-q', 'vjc-printclasspath']
63+
call delete(s:temporaryAntFile)
64+
let result = system(join(antCmd, " "))
65+
for line in split(result, '\n')
66+
let matches = matchlist(line, '\m^\s\+\[echo\]\s\+\(.*\)')
67+
if !empty(matches)
68+
return matches[1]
69+
endif
70+
endfor
71+
return '.'
72+
endfunction
73+
74+
" 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)