Grails 配置:包含另一个配置文件

发布于 2024-11-19 04:40:04 字数 257 浏览 3 评论 0原文

我的主配置中有类似:

grails.config.locations = ["file:grails-app/config/Jawr.groovy"]。

当使用 grails run-app 运行应用程序时,一切正常。

但是,在部署(创建战争档案)时,这不再起作用,因为文件“Jawr.groovy”不再保留在文件系统上(它应该只在战争中)。

你有解决办法吗?如何将外部文件包含到 grails 主配置文件中?

谢谢。

I have in my main config something like:

grails.config.locations = ["file:grails-app/config/Jawr.groovy"].

When running the application with grails run-app, everything is OK.

But, on deployment (creating the war archive) this does not work anymore, as the file "Jawr.groovy" is not kept anymore on the filesystem (it should be only in the war).

Do you have a solution for that? Hw do you include external files into the grails main configuration file?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

空心空情空意 2024-11-26 04:40:04

好的,这里有几件事。

首先,因为您的配置路径上没有前导斜杠,所以这是一个相对于谁知道在哪里的路径。我在 Tomcat 中使用了这个,该路径最终与启动 Tomcat 服务器时所在的工作目录相关。如果启动 Tomcat,关闭它,更改目录,然后再次启动它,您将获得两个不同的配置路径。

其次,grails-app 目录仅存在于 Grails 项目的源代码树中。解压后的 WAR 文件的结构更像 Grails 源代码树的 web-app 文件夹,其中包含 WEB-INF、META-INF、js、images 等文件夹。

第三,您可能希望避免放置外部化配置文件在你的 webapp 的文件夹中。下次部署应用程序时,该配置将被旧版本的应用程序删除。外部化配置的要点之一是您可以重新部署而无需重新配置。

一个简单但不太理想的解决方案是使用静态的完全限定路径,例如 /etc/yourApp/conf.groovy,然后将其放入文档中。还有一个插件可以处理这个问题。

http://www.grails.org/plugin/external-config

我没有使用过它,但描述使它听起来像是做了明智的事情。

Okay, a few things here.

First, because you don't have a leading slash on your config path, that is a path relative to who knows where. I played with this in Tomcat, and that path ends up being relative to the working directory you were in when starting the Tomcat server. If you start Tomcat, shut it down, change directories, then start it again, you are going to get two different config paths.

Second, the grails-app directory only exists within the source tree of your Grails project. The structure of an unpacked WAR file is more like the web-app folder of your Grails source tree, with folders like WEB-INF, META-INF, js, images, etc.

Third, you probably want to avoid putting your externalized config file inside the folder of your webapp. The next time you deploy your app, that configuration is going to get wiped away with the old version of the app. One of the points of the externalized config is so that you can redeploy without having to reconfigure.

A simple, but less than ideal, solution would be to use a static, fully qualified path, like /etc/yourApp/conf.groovy, then put that in the documentation. There is also a plug-in that handles this.

http://www.grails.org/plugin/external-config

I haven't used it, but the description makes it sound like it does sensible things.

○愚か者の日 2024-11-26 04:40:04

看到这个: https://stackoverflow .com/questions/6341117/is-it-possible-that-grails-xxconfig-groovy-as-a-script-no-compile

然后我把它放入/shared,然后
修改:

//Config.groovy
grails.config.locations =
[“文件:共享/TZLibConfig.groovy”]

//BuildConfig.groovy
grails.war.resources = { stagingDir, args ->;
    复制(todir:“$ {stagingDir} / WEB-INF /共享”){
        文件集(目录:“共享”,包括:“**”)
    }
}

see this: https://stackoverflow.com/questions/6341117/is-it-possible-that-grails-xxconfig-groovy-as-a-script-no-compile

Then I put it into /shared, and
modify:

//Config.groovy
grails.config.locations =
["file:shared/TZLibConfig.groovy"]

//BuildConfig.groovy
grails.war.resources = { stagingDir, args ->
    copy(todir: "${stagingDir}/WEB-INF/shared"){
        fileset(dir:"shared",includes:"**")
    }
}
江心雾 2024-11-26 04:40:04

在我的工作中,我们的团队经常使用系统属性来保存配置文件的路径(通常在运行应用程序的用户的主文件夹中 - 为了特权)。然后我们手动将配置文件复制到该路径中

为了识别它是生产环境,我们在Config.groovy中使用以下代码:

if (System.properties["${appName}.config.location"]) {
    grails.config.locations = ["file:" + System.properties["${appName}.config.location"]]
}

In my work, our team often use a system properties to save the path to the config file (often in home folder of the user running the app - for privilege sake). Then we manually copy the config file into that path

To identify that it's the production environment, we use the following code in Config.groovy:

if (System.properties["${appName}.config.location"]) {
    grails.config.locations = ["file:" + System.properties["${appName}.config.location"]]
}
草莓酥 2024-11-26 04:40:04

本文建议允许用户指定位置将配置文件作为环境变量或作为 java 属性 --- 这意味着您可以简单地在命令行上使用 -D 指定它。除了所有其他方法之外,还可以使用此方法。

This article suggests allowing the user to specify the location of the config file as an environment variable or as a java property --- meaning you can simply specify it with -D on the command-line. This can be used in addition to all the other methods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文