Grails Config: include another config file

2019-04-11 16:36发布

问题:

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.

回答1:

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.



回答2:

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:

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"]]
}


回答4:

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.