Skip to content
Newer
Older
100644 276 lines (236 sloc) 9.33 KB
3e17890 1.1 branch is now trunk
graeme authored Mar 13, 2009
1 /*
2 * Copyright 2004-2005 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
98733cf @graemerocher Partial fix for run-app in interactive mode, there seems to be anothe…
graemerocher authored Jun 8, 2011
16
3e17890 1.1 branch is now trunk
graeme authored Mar 13, 2009
17 import grails.util.GrailsUtil
f2f37f1 @burtbeckwith added port tests to avoid long stacktrace when there's already a serv…
burtbeckwith authored Jun 11, 2011
18 import grails.util.Metadata
3e17890 1.1 branch is now trunk
graeme authored Mar 13, 2009
19 import grails.web.container.EmbeddableServer
f2f37f1 @burtbeckwith added port tests to avoid long stacktrace when there's already a serv…
burtbeckwith authored Jun 11, 2011
20 import grails.web.container.EmbeddableServerFactory
21
3814049 @burtbeckwith GRAILS-7781 fixed CTRL-C issue using workaround in comments of https:…
burtbeckwith authored Aug 1, 2011
22 import java.awt.event.ActionEvent
23 import java.awt.event.ActionListener
f2f37f1 @burtbeckwith added port tests to avoid long stacktrace when there's already a serv…
burtbeckwith authored Jun 11, 2011
24 import java.net.ServerSocket
25
a4ebf48 @burtbeckwith whitespace/cleanup
burtbeckwith authored Jul 22, 2011
26 import org.codehaus.groovy.grails.cli.ScriptExitException
f2f37f1 @burtbeckwith added port tests to avoid long stacktrace when there's already a serv…
burtbeckwith authored Jun 11, 2011
27 import org.codehaus.groovy.grails.cli.interactive.InteractiveMode
4fd2ac3 @burtbeckwith whitespace, cleanup
burtbeckwith authored Jun 17, 2011
28 import org.codehaus.groovy.grails.compiler.GrailsProjectWatcher
3e17890 1.1 branch is now trunk
graeme authored Mar 13, 2009
29
30 /**
4fd2ac3 @burtbeckwith whitespace, cleanup
burtbeckwith authored Jun 18, 2011
31 * Gant script that executes Grails using an embedded server.
3e17890 1.1 branch is now trunk
graeme authored Mar 13, 2009
32 *
33 * @author Graeme Rocher
34 *
35 * @since 0.4
36 */
37
081c7a6 @graemerocher Grails 1.2 now uses Tomcat as the default server
graemerocher authored Jul 17, 2009
38 includeTargets << grailsScript("_GrailsPlugins")
3e17890 1.1 branch is now trunk
graeme authored Mar 13, 2009
39
835947f @burtbeckwith cleanup
burtbeckwith authored Jun 22, 2010
40 SCHEME_HTTP = "http"
41 SCHEME_HTTPS = "https"
3e17890 1.1 branch is now trunk
graeme authored Mar 13, 2009
42
b735e73 @pledbrook Fix for GRAILS-4364: automatic server restarts now remember whether
pledbrook authored Nov 18, 2009
43 // Keep track of whether we're running in HTTPS mode in case we need
44 // to restart the server.
45 usingSecureServer = false
3e17890 1.1 branch is now trunk
graeme authored Mar 13, 2009
46
47 grailsServer = null
48 grailsContext = null
49 autoRecompile = System.getProperty("disable.auto.recompile") ? !(System.getProperty("disable.auto.recompile").toBoolean()) : true
50
51 // How often should recompilation occur while the application is running (in seconds)?
52 // Defaults to 3s.
53 recompileFrequency = System.getProperty("recompile.frequency")
54 recompileFrequency = recompileFrequency ? recompileFrequency.toInteger() : 3
55
56 shouldPackageTemplates = true
57
58 // This isn't used within this script but may come in handy for scripts
59 // that depend on this one.
60 ant.path(id: "grails.runtime.classpath", runtimeClasspath)
61
62 /**
63 * Runs the application in dev mode, i.e. with class-reloading.
64 */
65 target(runApp: "Main implementation that executes a Grails application") {
66 runInline(SCHEME_HTTP, serverHost, serverPort, serverPortHttps)
67 }
68
69 /**
70 * Runs the application in dev mode over HTTPS.
71 */
72 target(runAppHttps: "Main implementation that executes a Grails application with an HTTPS listener") {
73 runInline(SCHEME_HTTPS, serverHost, serverPort, serverPortHttps)
74 }
75
76 /**
77 * Runs the application using the WAR file directly.
78 */
79 target (runWar : "Main implementation that executes a Grails application WAR") {
80 runWar(SCHEME_HTTP, serverHost, serverPort, serverPortHttps)
81 }
82
83 /**
84 * Runs the application over HTTPS using the WAR file directly.
85 */
86 target (runWarHttps : "Main implementation that executes a Grails application WAR") {
87 runWar(SCHEME_HTTPS, serverHost, serverPort, serverPortHttps)
88 }
89
90 private EmbeddableServerFactory loadServerFactory() {
91 def load = { name -> classLoader.loadClass(name).newInstance() }
92
7e60e71 @graemerocher Default tomcat server class name was incorrect
graemerocher authored May 19, 2011
93 String defaultServer = "org.grails.plugins.tomcat.TomcatServerFactory"
081c7a6 @graemerocher Grails 1.2 now uses Tomcat as the default server
graemerocher authored Jul 17, 2009
94 def containerClass = getPropertyValue("grails.server.factory", defaultServer)
3e17890 1.1 branch is now trunk
graeme authored Mar 13, 2009
95 EmbeddableServerFactory serverFactory
96 try {
97 serverFactory = load(containerClass)
98 }
835947f @burtbeckwith cleanup
burtbeckwith authored Jun 22, 2010
99 catch (ClassNotFoundException cnfe) {
100 if (containerClass == defaultServer) {
e3fb07d @burtbeckwith renamed 'console' binding variable to 'grailsConsole' since it confli…
burtbeckwith authored Jun 16, 2011
101 grailsConsole.error "WARNING: No default container found, installing Tomcat.."
6bbf23a @graemerocher fixed code causing MME
graemerocher authored May 11, 2010
102 doInstallPlugin "tomcat", GrailsUtil.grailsVersion
0bf3e1f @graemerocher Fixed by clearing plugin settings cache (wrong method signature)
graemerocher authored Oct 26, 2009
103 pluginSettings.clearCache()
081c7a6 @graemerocher Grails 1.2 now uses Tomcat as the default server
graemerocher authored Jul 17, 2009
104 compilePlugins()
6447df7 @graemerocher another fix for the failing functional tests
graemerocher authored May 11, 2010
105 loadPlugins()
835947f @burtbeckwith cleanup
burtbeckwith authored Jun 22, 2010
106 serverFactory = load(containerClass)
081c7a6 @graemerocher Grails 1.2 now uses Tomcat as the default server
graemerocher authored Jul 17, 2009
107 }
108 }
3e17890 1.1 branch is now trunk
graeme authored Mar 13, 2009
109 catch (Throwable e) {
e3fb07d @burtbeckwith renamed 'console' binding variable to 'grailsConsole' since it confli…
burtbeckwith authored Jun 16, 2011
110 grailsConsole.error e
3e17890 1.1 branch is now trunk
graeme authored Mar 13, 2009
111 event("StatusFinal", ["Failed to load container [$containerClass]: ${e.message}"])
112 exit(1)
113 }
114 return serverFactory
115 }
116
117 private runInline(scheme, host, httpPort, httpsPort) {
118 EmbeddableServerFactory serverFactory = loadServerFactory()
119 grailsServer = serverFactory.createInline("${basedir}/web-app", webXmlFile.absolutePath, serverContextPath, classLoader)
120 runServer server: grailsServer, host:host, httpPort: httpPort, httpsPort: httpsPort, scheme:scheme
121 startPluginScanner()
122 }
123
124 private runWar(scheme, host, httpPort, httpsPort) {
125 EmbeddableServerFactory serverFactory = loadServerFactory()
126 grailsServer = serverFactory.createForWAR(warName, serverContextPath)
127
f2f37f1 @burtbeckwith added port tests to avoid long stacktrace when there's already a serv…
burtbeckwith authored Jun 11, 2011
128 Metadata.getCurrent().put(Metadata.WAR_DEPLOYED, "true")
3e17890 1.1 branch is now trunk
graeme authored Mar 13, 2009
129 runServer server:grailsServer, host:host, httpPort:httpPort, httpsPort: httpsPort, scheme: scheme
130 }
131
132 /**
133 * Runs the Server. You can pass these named arguments:
134 *
135 * server - The server instance to use (required).
136 * port - The network port the server is running on (used to display the URL) (required).
137 * scheme - The network scheme to display in the URL (optional; defaults to "http").
138 */
139 runServer = { Map args ->
140 try {
1aa1cb0 @graemerocher Enhanced terminal output, needed to support redesigned interactive mode
graemerocher authored Jun 3, 2011
141 event("StatusUpdate", ["Running Grails application"])
3e17890 1.1 branch is now trunk
graeme authored Mar 13, 2009
142 def message = "Server running. Browse to http://${args.host ?: 'localhost'}:${args.httpPort}$serverContextPath"
143
144 EmbeddableServer server = args["server"]
835947f @burtbeckwith cleanup
burtbeckwith authored Jun 22, 2010
145 if (server.hasProperty('eventListener')) {
0f88cc9 @graemerocher registration of event listener and grailsConfig not tied to Jetty
graemerocher authored Sep 11, 2009
146 server.eventListener = this
147 }
835947f @burtbeckwith cleanup
burtbeckwith authored Jun 22, 2010
148 if (server.hasProperty('grailsConfig')) {
0f88cc9 @graemerocher registration of event listener and grailsConfig not tied to Jetty
graemerocher authored Sep 11, 2009
149 server.grailsConfig = config
3e17890 1.1 branch is now trunk
graeme authored Mar 13, 2009
150 }
151
152 profile("start server") {
f2f37f1 @burtbeckwith added port tests to avoid long stacktrace when there's already a serv…
burtbeckwith authored Jun 11, 2011
153
154 try { new ServerSocket(args.httpPort).close() }
155 catch (IOException e) {
1725fc8 better errors for test compilation and port binding
Graeme Rocher authored Jul 6, 2011
156 event("StatusError", ["Server failed to start for port $args.httpPort: $e.message"])
f2f37f1 @burtbeckwith added port tests to avoid long stacktrace when there's already a serv…
burtbeckwith authored Jun 11, 2011
157 exit(1)
158 }
159
835947f @burtbeckwith cleanup
burtbeckwith authored Jun 22, 2010
160 if (args.scheme == 'https') {
f2f37f1 @burtbeckwith added port tests to avoid long stacktrace when there's already a serv…
burtbeckwith authored Jun 11, 2011
161
162 try { new ServerSocket(args.httpsPort).close() }
163 catch (IOException e) {
1725fc8 better errors for test compilation and port binding
Graeme Rocher authored Jul 6, 2011
164 event("StatusError", ["Server failed to start for port $args.httpsPort: $e.message"])
f2f37f1 @burtbeckwith added port tests to avoid long stacktrace when there's already a serv…
burtbeckwith authored Jun 11, 2011
165 exit(1)
166 }
167
b735e73 @pledbrook Fix for GRAILS-4364: automatic server restarts now remember whether
pledbrook authored Nov 18, 2009
168 usingSecureServer = true
3e17890 1.1 branch is now trunk
graeme authored Mar 13, 2009
169 server.startSecure args.host, args.httpPort, args.httpsPort
170
171 // Update the message to reflect the fact we are running HTTPS as well.
172 message += " or https://${args.host ?: 'localhost'}:${args.httpsPort}$serverContextPath"
173 }
174 else {
175 server.start args.host, args.httpPort
176 }
177 }
178 event("StatusFinal", [message])
3814049 @burtbeckwith GRAILS-7781 fixed CTRL-C issue using workaround in comments of https:…
burtbeckwith authored Aug 1, 2011
179
94733fb @burtbeckwith whitespace, STS warnings, etc.
burtbeckwith authored Aug 31, 2011
180 boolean isWindows = System.getProperty("os.name").toLowerCase().indexOf("windows") != -1
181 if (isWindows) {
e44a0e5 @graemerocher fix for exception in MAVEN-162
graemerocher authored Apr 20, 2012
182 grailsConsole?.reader?.addTriggeredAction((char)3, new ActionListener() {
94733fb @burtbeckwith whitespace, STS warnings, etc.
burtbeckwith authored Aug 31, 2011
183 void actionPerformed(ActionEvent e) {
184 stopServer()
185 exit(0)
186 }
187 })
188 }
835947f @burtbeckwith cleanup
burtbeckwith authored Jun 22, 2010
189 }
190 catch (Throwable t) {
a4ebf48 @burtbeckwith whitespace/cleanup
burtbeckwith authored Jul 22, 2011
191 if (t instanceof ScriptExitException) throw t
3e17890 1.1 branch is now trunk
graeme authored Mar 13, 2009
192 GrailsUtil.deepSanitize(t)
835947f @burtbeckwith cleanup
burtbeckwith authored Jun 22, 2010
193 if (!(t instanceof SocketException) && !(t.cause instanceof SocketException)) {
e3fb07d @burtbeckwith renamed 'console' binding variable to 'grailsConsole' since it confli…
burtbeckwith authored Jun 16, 2011
194 grailsConsole.error t
835947f @burtbeckwith cleanup
burtbeckwith authored Jun 22, 2010
195 }
26717f7 improved error message if server fails to start
Graeme Rocher authored Jul 14, 2011
196 event("StatusError", ["Server failed to start: $t"])
3e17890 1.1 branch is now trunk
graeme authored Mar 13, 2009
197 exit(1)
198 }
199 }
200
201 /**
202 * Starts the plugin scanner. Call this after starting the server if you
203 * want changes to artifacts automatically detected and loaded.
204 */
205 target(startPluginScanner: "Starts the plugin manager's scanner that detects changes to artifacts.") {
627b0d5 @graemerocher fix for GRAILS-8769 ""No property found for name" exception on unit t…
graemerocher authored Feb 14, 2012
206 if(GrailsProjectWatcher.isReloadingAgentPresent() && !GrailsProjectWatcher.isActive()) {
207 projectWatcher = new GrailsProjectWatcher(projectCompiler, pluginManager)
208 projectWatcher.start()
209 }
3e17890 1.1 branch is now trunk
graeme authored Mar 13, 2009
210 }
211
6779eb9 @alkemist When stopping the running server, also stop the plugin change scanner.
alkemist authored Oct 27, 2009
212 target(stopPluginScanner: "Stops the plugin manager's scanner that detects changes to artifacts.") {
4a69917 @burtbeckwith whitespace, STS warnings, etc.
burtbeckwith authored May 27, 2011
213 // do nothing, here for compatibility
6779eb9 @alkemist When stopping the running server, also stop the plugin change scanner.
alkemist authored Oct 27, 2009
214 }
215
3e17890 1.1 branch is now trunk
graeme authored Mar 13, 2009
216 /**
217 * Keeps the server alive and checks for changes in domain classes or
218 * source files under "src". If any changes are detected, the servlet
219 * container is restarted.
220 */
221 target(watchContext: "Watches the WEB-INF/classes directory for changes and restarts the server if necessary") {
222 depends(classpath)
223
98733cf @graemerocher Partial fix for run-app in interactive mode, there seems to be anothe…
graemerocher authored Jun 8, 2011
224 if (InteractiveMode.current) {
f2f37f1 @burtbeckwith added port tests to avoid long stacktrace when there's already a serv…
burtbeckwith authored Jun 11, 2011
225 Thread.start {
226 def im = InteractiveMode.current
227 im.grailsServer = grailsServer
228 im.run()
229 }
3e17890 1.1 branch is now trunk
graeme authored Mar 13, 2009
230 }
231
4a69917 @burtbeckwith whitespace, STS warnings, etc.
burtbeckwith authored May 27, 2011
232 keepServerAlive()
3e17890 1.1 branch is now trunk
graeme authored Mar 13, 2009
233 }
234
235 target(keepServerAlive: "Idles the script, ensuring that the server stays running.") {
236 def keepRunning = true
237 def killFile = new File("${basedir}/.kill-run-app")
1f25b63 @pledbrook Updated keepServerAlive target so that if the kill file already exist…
pledbrook authored Jul 27, 2011
238 if (killFile.exists()) {
239 grailsConsole.warning ".kill-run-app file exists - perhaps a previous server stop didn't work?. Deleting and continuing anyway."
240 killFile.delete()
241 }
242
3e17890 1.1 branch is now trunk
graeme authored Mar 13, 2009
243 while (keepRunning) {
244 sleep(recompileFrequency * 1000)
245
246 // Check whether the kill file exists. This is a hack for the
247 // functional tests so that we can stop the servers that are
248 // started.
249 if (killFile.exists()) {
e3fb07d @burtbeckwith renamed 'console' binding variable to 'grailsConsole' since it confli…
burtbeckwith authored Jun 16, 2011
250 grailsConsole.updateStatus "Stopping server..."
3e17890 1.1 branch is now trunk
graeme authored Mar 13, 2009
251 grailsServer.stop()
252 killFile.delete()
253 keepRunning = false
254 }
255 }
256 }
257
69c8376 @graemerocher Fix for GRAILS-5055 "Server parameter to eventConfigureJetty is null …
graemerocher authored Sep 11, 2009
258 target(stopServer: "Stops the Grails servlet container") {
3e17890 1.1 branch is now trunk
graeme authored Mar 13, 2009
259 if (grailsServer) {
5518da2 @graemerocher handle server stoppage errors more elegantly
graemerocher authored Sep 11, 2009
260 try {
261 grailsServer.stop()
262 }
263 catch (Throwable e) {
e3fb07d @burtbeckwith renamed 'console' binding variable to 'grailsConsole' since it confli…
burtbeckwith authored Jun 16, 2011
264 grailsConsole.error "Error stopping server: ${e.message}", e
5518da2 @graemerocher handle server stoppage errors more elegantly
graemerocher authored Sep 11, 2009
265 }
835947f @burtbeckwith cleanup
burtbeckwith authored Jun 22, 2010
266
6779eb9 @alkemist When stopping the running server, also stop the plugin change scanner.
alkemist authored Oct 27, 2009
267 try {
268 stopPluginScanner()
269 }
270 catch (Throwable e) {
e3fb07d @burtbeckwith renamed 'console' binding variable to 'grailsConsole' since it confli…
burtbeckwith authored Jun 16, 2011
271 grailsConsole.error "Error stopping plugin change scanner: ${e.message}", e
6779eb9 @alkemist When stopping the running server, also stop the plugin change scanner.
alkemist authored Oct 27, 2009
272 }
3e17890 1.1 branch is now trunk
graeme authored Mar 13, 2009
273 }
274 event("StatusFinal", ["Server stopped"])
275 }
Something went wrong with that request. Please try again.