Skip to content

Commit 5f06cb5

Browse files
author
marcpalmer
committed
GRAILS-1236 and GRAILS-1210 - can now modify classpath used for grails app from Events.groovy scripts, and repaired Event scripts in 0.5.5
git-svn-id: https://svn.codehaus.org/grails/trunk@4504 1cfb16fd-6d17-0410-8ff1-b7e8e1e2867d
1 parent 2f4d0e6 commit 5f06cb5

File tree

1 file changed

+52
-22
lines changed

1 file changed

+52
-22
lines changed

scripts/Init.groovy

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,28 @@ baseFile = new File(basedir)
4141
baseName = baseFile.name
4242
userHome = Ant.antProject.properties."user.home"
4343
grailsTmp = "${userHome}/.grails/tmp"
44+
eventsClassLoader = new GroovyClassLoader(getClass().classLoader)
4445

4546
resolver = new PathMatchingResourcePatternResolver()
4647
grailsAppName = null
4748
appGrailsVersion = null
4849
hookScripts = [this]
49-
50-
loadEventHooks()
50+
hooksLoaded = false
51+
classpathSet = false
5152

5253
// Send a scripting event notification to any and all event hooks in plugins/user scripts
5354
event = { String name, def args ->
55+
if (!hooksLoaded) {
56+
setClasspath()
57+
58+
loadEventHooks()
59+
60+
hooksLoaded = true
61+
62+
// Give scripts a chance to modify classpath
63+
event('setClasspath', [getClass().classLoader.rootLoader])
64+
}
65+
5466
hookScripts.each() {
5567
try {
5668
def handler = it."event$name"
@@ -78,6 +90,9 @@ eventCreatedArtefact = { artefactType, artefactName ->
7890
}
7991

8092
/* Standard handlers not handled by Init
93+
eventSetClasspath = { rootLoader ->
94+
// Called when root classloader is being configured
95+
}
8196
eventCreatedFile = { fileName ->
8297
// Called when any file is created in the project tree, that is to be part of the project source (not regenerated)
8398
}
@@ -112,12 +127,13 @@ void loadEventHooks() {
112127

113128
void loadEventScript(theFile) {
114129
try {
115-
def script = getClass().classLoader.parseClass( theFile).newInstance()
130+
def script = eventsClassLoader.parseClass( theFile).newInstance()
116131
script.delegate = binding
117132
script.run()
118133
hookScripts << script
119134
} catch (Throwable t) {
120135
println "Unable to load event script $theFile: ${t.message}"
136+
t.printStackTrace()
121137
}
122138
}
123139

@@ -153,7 +169,8 @@ resolveResources = { String pattern ->
153169
return resolver.getResources(pattern)
154170
}
155171
catch(Exception e) {
156-
return []
172+
e.printStackTrace()
173+
return []
157174
}
158175
}
159176

@@ -364,14 +381,20 @@ task(promptForName:"Prompts the user for the name of the Artifact if it isn't sp
364381
}
365382

366383
task(classpath:"Sets the Grails classpath") {
367-
def grailsDir = resolveResources("file:${basedir}/grails-app/*")
368-
384+
setClasspath()
385+
}
386+
387+
void setClasspath() {
388+
if (classpathSet) return
389+
390+
def grailsDir = resolveResources("file:${basedir}/grails-app/*")
391+
369392
Ant.path(id:"grails.classpath") {
370-
pathelement(location:"${basedir}")
371-
pathelement(location:"${basedir}/grails-tests")
393+
pathelement(location:"${basedir}")
394+
pathelement(location:"${basedir}/grails-tests")
372395
pathelement(location:"${basedir}/web-app")
373396
pathelement(location:"${basedir}/web-app/WEB-INF")
374-
pathelement(location:"${basedir}/web-app/WEB-INF/classes")
397+
pathelement(location:"${basedir}/web-app/WEB-INF/classes")
375398
if (new File("${basedir}/web-app/WEB-INF/lib").exists()) {
376399
fileset(dir:"${basedir}/web-app/WEB-INF/lib")
377400
}
@@ -380,43 +403,50 @@ task(classpath:"Sets the Grails classpath") {
380403
fileset(dir:"lib")
381404
for(d in grailsDir) {
382405
pathelement(location:"${d.file.absolutePath}")
383-
}
406+
}
384407
}
385408
StringBuffer cpath = new StringBuffer("")
386409

387410
def jarFiles = resolveResources("lib/*.jar").toList()
388411

389-
resolveResources("plugins/*/lib/*.jar").each { pluginJar ->
412+
resolveResources("plugins/*/lib/*.jar").each { pluginJar ->
390413
boolean matches = jarFiles.any { it.file.name == pluginJar.file.name }
391414
if(!matches) jarFiles.add(pluginJar)
392415
}
393-
resolveResources("${grailsHome}/lib/junit-*.jar").each { testJar ->
394-
jarFiles.add(testJar)
395-
}
396-
416+
417+
resolveResources("file:${userHome}/.grails/lib/*.jar").each { userJar ->
418+
jarFiles.add(userJar)
419+
}
420+
421+
422+
397423
def rootLoader = getClass().classLoader.rootLoader
398424

399425
jarFiles.each { jar ->
400426
cpath << jar.file.absolutePath << File.pathSeparator
401-
rootLoader?.addURL(jar.URL)
402-
}
427+
rootLoader?.addURL(jar.URL)
428+
}
403429

404430
grailsDir.each { dir ->
405-
cpath << dir.file.absolutePath << File.pathSeparator
431+
cpath << dir.file.absolutePath << File.pathSeparator
406432
rootLoader?.addURL(dir.URL)
407-
}
433+
}
408434

409-
cpath << "${basedir}/web-app/WEB-INF/classes"
435+
cpath << "${basedir}/web-app/WEB-INF/classes"
410436
rootLoader?.addURL(new File("${basedir}/web-app/WEB-INF/classes").toURL())
411437
cpath << "${basedir}/web-app/WEB-INF"
412438
rootLoader?.addURL(new File("${basedir}/web-app/WEB-INF").toURL())
413-
414439

415440
// println "Classpath with which to generate web.xml: \n${cpath.toString()}"
416441

417442
parentLoader = getClass().getClassLoader()
443+
418444
compConfig = new CompilerConfiguration()
419-
compConfig.setClasspath(cpath.toString());
445+
compConfig.setClasspath(cpath.toString());
446+
447+
classpathSet = true
448+
449+
event('setClasspath', [rootLoader])
420450
}
421451

422452
task( configureProxy: "The implementation task") {

0 commit comments

Comments
 (0)