Grails Config: include another config file

2019-04-11 16:39发布

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.

4条回答
一纸荒年 Trace。
2楼-- · 2019-04-11 17:17

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:"**")
    }
}
查看更多
一夜七次
3楼-- · 2019-04-11 17:22

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.

查看更多
叼着烟拽天下
4楼-- · 2019-04-11 17:25

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"]]
}
查看更多
男人必须洒脱
5楼-- · 2019-04-11 17:34

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.

查看更多
登录 后发表回答