6.2.7 修改已经部署的应用 - Reference Documentation
Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith
Version: null
6.2.7 修改已经部署的应用
One of the main issues with deploying a Grails application (or typically any servlet-based one) is that any change to the views requires that you redeploy your whole application. If all you want to do is fix a typo on a page, or change an image link, it can seem like a lot of unnecessary work. For such simple requirements, Grails does have a solution: the
The first line tells Grails that modified GSP files should be reloaded at runtime. If you don't have this setting, you can make as many changes as you like but they won't be reflected in the running application until you restart. The second line tells Grails where to load the views and layouts from.
The key point here is that you must retain the view directory structure, including the
GSP reloading is supported for precompiled GSPs since Grails 1.3.5 .
部署一个Grails应用(或者任意基于servlet的应用)的一个主要问题是视图的任何修改都需要重新再部署你的整个应用。假如你只是想修复一个页面的打字错误或者修改一个图像链接,那么这种再部署像是比较多余的工作。对于这种比较简单的需求,Grails提供了一个解决方案:配置grails.gsp.view.dir configuration setting.How does this work? The first step is to decide where the GSP files should go. Let's say we want to keep them unpacked in a /var/www/grails/my-app directory. We add these two lines to grails-app/conf/Config.groovy :
grails.gsp.enable.reload = true grails.gsp.view.dir = "/var/www/grails/my-app/"
The trailing slash on the grails.gsp.view.dir value is important! Without it, Grails will look for views in the parent directory.
Setting "grails.gsp.view.dir" is optional. If it's not specified, you can update files directly to the application server's deployed war directory. Depending on the application server, these files might get overwritten when the server is restarted. Most application servers support "exploded war deployment" which is recommended in this case.With those settings in place, all you need to do is copy the views from your web application to the external directory. On a Unix-like system, this would look something like this:
mkdir -p /var/www/grails/my-app/grails-app/views cp -R grails-app/views/* /var/www/grails/my-app/grails-app/views
grails-app/views bit. So you end up with the path /var/www/grails/my-app/grails-app/views/... .One thing to bear in mind with this technique is that every time you modify a GSP, it uses up permgen space. So at some point you will eventually hit "out of permgen space" errors unless you restart the server. So this technique is not recommended for frequent or large changes to the views.There are also some System properties to control GSP reloading:
| Name | Description | Default |
|---|---|---|
| grails.gsp.enable.reload | altervative system property for enabling the GSP reload mode without changing Config.groovy | |
| grails.gsp.reload.interval | interval between checking the lastmodified time of the gsp source file, unit is milliseconds | 5000 |
| grails.gsp.reload.granularity | the number of milliseconds leeway to give before deciding a file is out of date. this is needed because different roundings usually cause a 1000ms difference in lastmodified times | 1000 |
grails.gsp.view.dir属性。那么它是如何工作的呢?第一步就是要确定GSP文件位于什么地方。假设我们想让这些文件解压缩到/var/www/grails/my-app目录,那么我们需要在grails-app/conf/Config.groovy增加如下两行:
grails.gsp.enable.reload = true grails.gsp.view.dir = "/var/www/grails/my-app/"
grails.gsp.view.dir值的最后一个反斜杠是很重要的!没有它,Grails将会在其上一级目录寻找视图。
"grails.gsp.view.dir"的值是可选的。如果没有设置,你可以直接更新部署在应用服务器的war目录下的文件。这些文件可能会在应用服务器重新启动的时候被覆盖,不过这是跟服务器相关的。在这个时候,大部分的应用服务器所支持的“war额外加载部署(exploded war deployment)”模式是值得推荐的。所有这些设置完毕以后,你所需要做的就是从你的web应用中拷贝视图文件到外部的目录中。在一个Unix类的系统中,这可能看起来如下所示:
mkdir -p /var/www/grails/my-app/grails-app/views cp -R grails-app/views/* /var/www/grails/my-app/grails-app/views
grails-app/views本身。因此你的路径是/var/www/grails/my-app/grails-app/views/...的形式。使用此技术,要牢记的一件事情是在你每一次修改GSP文件的时候,会增加permgen的内存空间。因此最终你将会碰到"permgen内存空间益"的错误,当然你可以通过重新启动服务器来解决。所以,此技术不推荐应用于视图被频繁或者大量修改的情况。此外还有一些系统级的属性配置来控制GSP的重新加载:
| 名称 | 描述 | 缺省值 |
|---|---|---|
| grails.gsp.enable.reload | 在不修改Config.groovy的情况下,通过系统设置变量来启动GSP重栽模式 | |
| grails.gsp.reload.interval | 轮询gsp源文件最后修改时间的时间间隔,单位是毫秒 | 5000 |
| grails.gsp.reload.granularity | 在一个文件超时以前预留的毫秒数,此项是需要的,因为最后修改时间精度会导致1000毫秒的误差 | 1000 |

