在grails中构建特定于环境的web.xml
条目的最佳方法是什么?
我需要对生产进行某些修改,因为它们会在本地运行。
有什么想法吗?
答案 0 :(得分:15)
您可以使用“WebXmlEnd”事件的事件处理程序创建scripts/_Events.groovy
,一旦Grails和插件完成更改,就会触发该事件。通过解析XML并写出更新的文件,使用普通搜索/替换或通过DOM方法更新XML:
import grails.util.Environment
eventWebXmlEnd = { String filename ->
if (Environment.current != Environment.PRODUCTION) {
return
}
String content = webXmlFile.text
// update the XML
content = ...
webXmlFile.withWriter { file -> file << content }
}
答案 1 :(得分:8)
这是我正在使用的解决方案,来自death-head.ch上的那个人
首先安装模板
grails install-templates
然后自定义您在web.xml
中找到的src/templates/war/web.xml
。我选择制作web_dev.xml
和web_prod.xml
并删除web.xml
。我希望web_prod.xml
包含安全约束块。反正...
将以下内容放在BuildConfig.groovy
:
// #########################################################
// ## Can't use environment switching block because BuildConfig doesn't support it.
// ## @url http://jira.grails.org/browse/GRAILS-4260
// ## So use this workaround:
// ## @url http://death-head.ch/blog/2010/09/finally-solved-the-base-authentication-in-grails/
// #########################################################
switch ("${System.getProperty('grails.env')}") {
case "development":
if (new File("/${basedir}/src/templates/war/web_dev.xml").exists()) {
grails.config.base.webXml = "file:${basedir}/src/templates/war/web_dev.xml"
}
break;
default:
if (new File("/${basedir}/src/templates/war/web_prod.xml").exists()) {
grails.config.base.webXml = "file:${basedir}/src/templates/war/web_prod.xml"
}
break;
}
祝你好运!
答案 2 :(得分:0)
我从未尝试过,但应可以在grails.config.base.webXml
中指定BuildConfig.groovy
参数,具体取决于当前环境。
有一个list of available BuildConfig settings here
修改
实际上,due to this issue,这不是前进的方式:-(也许通过财产如:
grails -Dgrails.config.base.webXml=/path/to/web.xml
这一切都可能吗?