Ever needed to run an existing java web application side by side with your grails project? I found myself in exactly that position this week, and discovered that it (like most things in grails) was rather simple to accomplish. Considering I work in a predominately Java environment, I often need to run a java web application side by side with my grails work. To date, I would simply run grails in the embedded tomcat engine (or Jetty in grails <1.2) bundled with grails, and use a local install of tomcat to power my java web apps.
I wanted to see if I could streamline the environment, and have my locally installed tomcat run my grails application. It is possible to use the excellent tomcat plugin to export a WAR of your project to an external tomcat instance via tomcat's manager application. The problem with this configuration is that you lose the "hot-deploy" features of grails which was a deal breaker for me.
After quite a bit of head scratching, googling, and yelling at my computer, I finally got pointed in the right direction from Graeme Rocher via the grails-user mailing list. If you are not aware, grails uses tomcat's embedded version under the hood as of 1.2. I was aware that grails emits numerous lifecycle events that you can hook into, but was unaware that the tomcat plugin uses this system to announce a "TomcatConfigured" event that exposes the underlying tomcat API. During startup of your grails project, the tomcat plugin will configure the embedded engine, and then announce the "TomcatConfigured" event, and pass along the newly configured tomcat. This can be used to manage and configure every aspect of the tomcat engine.
My current implementation supports only one specific web application. In the future, I plan on updating this code to support multiple applications which will be configurable via Config.groovy, which should be rather trivial.
Begin by adding your project specific configuration to Config.groovy:
grails.myproject.contextRoot = "/myproject"
grails.myproject.build.path = "/myproject/WEB-INF/classes"
grails.myproject.web.root = "/myproject/"
Next we need to add the event handler to our Grails application. This is done by simply creating a new file named _Events.groovy and placing it in the /scripts directory of your project.
import org.apache.catalina.*
import org.apache.catalina.connector.*
import org.apache.catalina.loader.WebappLoader
import org.codehaus.groovy.grails.commons.ConfigurationHolder
eventConfigureTomcat = {tomcat ->
println "### Starting load of custom application"
def contextRoot = ConfigurationHolder.config.grails.myproject.contextRoot
def buildroot= ConfigurationHolder.config.grails.myproject.build.path
def webroot = ConfigurationHolder.config.grails.myproject.web.root
File appDir = new File(webroot);
context = tomcat.addWebapp(contextRoot, appDir.getAbsolutePath());
context.reloadable = true
WebappLoader loader = new WebappLoader(tomcat.class.classLoader)
loader.addRepository(new File(buildroot).toURI().toURL().toString());
context.loader = loader
loader.container = context
println "### Ending load of custom application"
}
You can now start you application with the grails run-app command, and your java web application will be available under the context you set in Config.groovy.
As a side note, if you need to support having an apache web server sit in fron of your application, you can add the following to turn on AJP connections for the embedded tomcat container.
// enable AJP to allow apache to front tomcat
def ajpConnector = new Connector("org.apache.jk.server.JkCoyoteHandler")
ajpConnector.port = 8009
ajpConnector.setProperty("redirectPort", "8443")
ajpConnector.setProperty("protocol", "AJP/1.3")
ajpConnector.setProperty("enableLookups", "false")
tomcat.service.addConnector ajpConnector
Add this part right after the "context.reloadable = true" line.
注: grails内部自带的tomcat没有server.xml文件。
原文地址:
http://krollrich.blogspot.com/2009/12/run-java-web-application-within-grails.html
分享到:
相关推荐
### Grails 1.1 Web Application Development #### 核心知识点概述 《Grails 1.1 Web Application Development》是一本专注于使用Grails框架进行高效Web应用开发的技术书籍。本书由作者Jon Dickinson撰写,旨在...
它的易学性、强大的IDE支持、领域驱动开发、高性能、与Java EE平台的深度整合、活跃的社区支持、丰富的插件系统、约定优于配置的原则、重用已有经验的能力以及与其他Web框架的良好集成,共同构成了Grails作为Java...
### Grails 快速开发 Web 应用程序 #### 一、Grails 概述 Grails 是一种基于 Groovy 的开源...通过掌握 Grails 的核心概念和技术,开发者可以更快地构建高质量的 Web 应用程序,并在 Java 生态系统中发挥更大的作用。
### Grails快速开发Web应用:知识点详解 #### Grails框架概览 Grails是一个基于Groovy语言构建的开源MVC(Model-View-Controller)Web开发框架,以其高效的开发速度和简洁的代码著称。其核心优势在于: 1. **快速...
书的最后,作者将审视行业最佳实践以及这些最佳实践如何适用于你的书店Web应用程序,同时还包括了探讨其他Java Web框架的内容,如Groovy/Grails和Scala/Play2。在附录中,你还可以探索Java、Groovy和Scala的基础知识...
2. 安装配置:首先需要安装Java Development Kit(JDK),然后通过Grails官方下载页面获取最新版本的Grails,并将其添加到系统路径中。 3. 创建项目:使用`grails create-app`命令创建新项目,Grails会自动生成项目...
《使用 Grails 快速开发 Web 应用程序》 Grails,一个基于Groovy动态语言的开源MVC框架,为Web开发提供了高效且简洁的解决方案。自2007年发布以来,Grails以其快速开发能力,降低了Web应用的复杂性,吸引了众多...
自己买的书,然后用扫描机扫描的,整个文件太大了,不能一次性上传上来,所以拆成3个part。 我自己学grails很想看这本书,结果网上没有,就自己去买了,然后共享给需要的人。 如果有什么问题请联系我下架。
### Grails快速开发Web应用程序知识点解析 #### 一、Grails框架概述 - **定义**:Grails是一个基于Groovy语言构建的开源Model-View-Controller (MVC) Web开发框架。它旨在简化Web应用程序的开发流程,提高开发效率...
自己买的书,然后用扫描机扫描的,整个文件太大了,不能一次性上传上来,所以拆成3个part。...我自己学grails很想看这本书,结果网上没有,就自己去买了,然后共享给需要的人。 如果有什么问题请联系我下架。
Grails是一个基于Java平台的开源Web应用框架,它采用Groovy语言,提供了MVC(Model-View-Controller)架构模式,简化了开发过程。本项目是一个关于Grails的演示示例,特别关注了数据模型的一对多和多对多关系的配置...
Grails是一个基于Groovy语言的全栈Web应用开发框架,它遵循敏捷开发的理念,并且简化了基于Java的Web开发。Grails的核心是基于约定优于配置的原则,它提供了一套自动化的方式来处理项目的结构、数据持久化和Web层的...
Grails就是一个基于Groovy语言的开源Web应用框架,它以其简洁的语法和强大的功能受到开发者的欢迎。与此同时,Eclipse作为一款强大的Java集成开发环境(IDE),也提供了丰富的插件支持,帮助开发者更高效地进行...
本文将详细介绍8种流行的Java Web框架,包括Grails、GWT、JSF、Play、Spring、Struts、Vaadin和Wicket。 1. **Grails** Grails是一个基于Groovy语言的开源框架,它提供了快速开发Web应用的能力。安装Grails时,需...
像Rails,Django和TurboGears这样的动态框架在Web开发领域开辟了一条新的道路,Grails基于这些概念之上,采用动态方法减小了Java平台上进行Web开发的复杂度,不过与那些框架不同的是,Grails是构建在Spring和...
Grails1.1最新 中文 文档 当今的Java Web开发技术...Grails建立在这些概念之上,它极大地降低了在Java平台上建立Web应用的复杂性。与那些框架不同的是,Grails是构建在现有的像Spring、Hibernate这样的Java技术之上。
Grails建立在这些概念之上,它极大地降低了在Java平台上建立Web应用的复杂性。与那些框架不同的是,Grails是构建在现有的像Spring、Hibernate这样的Java技术之上。 Grails是个一栈式开发框架,它尝试通过核心技术...