@@ -39,12 +39,77 @@ grailsServer = null
3939grailsContext = null
4040autoRecompile = System . getProperty(" disable.auto.recompile" ) ? ! (System . getProperty(" disable.auto.recompile" ). toBoolean()) : true
4141
42+ // How often should recompilation occur while the application is running (in seconds)?
43+ // Defaults to 3s.
44+ recompileFrequency = System . getProperty(" recompile.frequency" )
45+ recompileFrequency = recompileFrequency ? recompileFrequency. toInteger() : 3
46+
4247
4348includeTargets << new File ( " ${ grailsHome} /scripts/Package.groovy" )
4449includeTargets << new File ( " ${ grailsHome} /scripts/PackagePlugins.groovy" )
4550
4651shouldPackageTemplates= true
4752
53+
54+ // Checks whether the project's sources have changed since the last
55+ // compilation, and then performs a recompilation if this is the case.
56+ // Returns the updated 'lastModified' value.
57+ recompileCheck = { lastModified ->
58+ try {
59+ def ant = new AntBuilder ()
60+ ant. taskdef ( name : ' groovyc' ,
61+ classname : ' org.codehaus.groovy.grails.compiler.GrailsCompiler' )
62+ def grailsDir = resolveResources(" file:${ basedir} /grails-app/*" )
63+ def pluginLibs = resolveResources(" file:${ basedir} /plugins/*/lib" )
64+ ant. path(id :" grails.classpath" ,grailsClasspath. curry(pluginLibs, grailsDir))
65+
66+ ant. groovyc(destdir :classesDirPath,
67+ classpathref :" grails.classpath" ,
68+ resourcePattern :" file:${ basedir} /**/grails-app/**/*.groovy" ,
69+ projectName :baseName) {
70+ src(path :" ${ basedir} /src/groovy" )
71+ src(path :" ${ basedir} /grails-app/domain" )
72+ src(path :" ${ basedir} /src/java" )
73+ javac(classpathref :" grails.classpath" , debug :" yes" )
74+
75+ }
76+ ant = null
77+ }
78+ catch (Exception e) {
79+ compilationError = true
80+ event(" StatusUpdate" , [" Error automatically restarting container: ${ e.message} " ])
81+ e. printStackTrace()
82+
83+ }
84+
85+ def tmp = classesDir. lastModified()
86+ if (lastModified < tmp) {
87+
88+ // run another compile JIT
89+ try {
90+ grailsServer. stop()
91+ compile()
92+ ClassLoader contextLoader = Thread . currentThread(). getContextClassLoader()
93+ classLoader = new URLClassLoader ([classesDir. toURL()] as URL [], contextLoader)
94+ // reload plugins
95+ loadPlugins()
96+ setupWebContext()
97+ grailsServer. setHandler( webContext )
98+ grailsServer. start()
99+ }
100+ catch (Exception e) {
101+ event(" StatusUpdate" , [" Error automatically restarting container: ${ e.message} " ])
102+ e. printStackTrace()
103+ }
104+
105+ finally {
106+ lastModified = classesDir. lastModified()
107+ }
108+ }
109+
110+ return lastModified
111+ }
112+
48113target (' default' : " Run's a Grails application in Jetty" ) {
49114 depends( checkVersion, configureProxy, packagePlugins, packageApp )
50115 runApp()
@@ -66,58 +131,11 @@ target ( runApp : "Main implementation that executes a Grails application") {
66131}
67132target( watchContext : " Watches the WEB-INF/classes directory for changes and restarts the server if necessary" ) {
68133 long lastModified = classesDir. lastModified()
69- while (true && autoRecompile) {
70- try {
71- def ant = new AntBuilder ()
72- ant. taskdef ( name : ' groovyc' ,
73- classname : ' org.codehaus.groovy.grails.compiler.GrailsCompiler' )
74- def grailsDir = resolveResources(" file:${ basedir} /grails-app/*" )
75- def pluginLibs = resolveResources(" file:${ basedir} /plugins/*/lib" )
76- ant. path(id :" grails.classpath" ,grailsClasspath. curry(pluginLibs, grailsDir))
77-
78- ant. groovyc(destdir :classesDirPath,
79- classpathref :" grails.classpath" ,
80- resourcePattern :" file:${ basedir} /**/grails-app/**/*.groovy" ,
81- projectName :baseName) {
82- src(path :" ${ basedir} /src/groovy" )
83- src(path :" ${ basedir} /grails-app/domain" )
84- src(path :" ${ basedir} /src/java" )
85- javac(classpathref :" grails.classpath" , debug :" yes" )
86-
87- }
88- ant = null
89- }
90- catch (Exception e) {
91- compilationError = true
92- event(" StatusUpdate" , [" Error automatically restarting container: ${ e.message} " ])
93- e. printStackTrace()
94-
95- }
96- def tmp = classesDir. lastModified()
97- if (lastModified < tmp) {
98-
99- // run another compile JIT
100- try {
101- grailsServer. stop()
102- compile()
103- ClassLoader contextLoader = Thread . currentThread(). getContextClassLoader()
104- classLoader = new URLClassLoader ([classesDir. toURL()] as URL [], contextLoader)
105- // reload plugins
106- loadPlugins()
107- setupWebContext()
108- grailsServer. setHandler( webContext )
109- grailsServer. start()
110- }
111- catch (Exception e) {
112- event(" StatusUpdate" , [" Error automatically restarting container: ${ e.message} " ])
113- e. printStackTrace()
114- }
115-
116- finally {
117- lastModified = classesDir. lastModified()
118- }
134+ while (true ) {
135+ if (autoRecompile) {
136+ lastModified = recompileCheck(lastModified)
119137 }
120- sleep(3000 )
138+ sleep(recompileFrequency * 1000 )
121139 }
122140}
123141
0 commit comments