Grails外部配置文件 [英] Grails external configuration file

查看:152
本文介绍了Grails外部配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用现有的Grails项目,这是处理外部配置文件的配置文件(Config.groovy)的一部分:

I started working with an existing Grails project and this is the part of the config file (Config.groovy) that deals with external configuration files:

grails.config.locations = [];

def defaultConfigFile = "${basedir}/configs/BombayConfig.groovy"
def defaultDatasourceFile = "${basedir}/configs/BombayDataSource.groovy"

if(File.separator == "\\") {
    defaultConfigFile = defaultConfigFile.replace('/', '\\')
    defaultDatasourceFile = defaultDatasourceFile.replace('/', '\\')
}

String PROJECT_NAME = "BOMBAY"
String CONFIG_ENV = "${PROJECT_NAME}_CONFIG_LOCATION"
String DATASOURCE_ENV = "${PROJECT_NAME}_DATASOURCE_LOCATION"

def externalConfig = System.getenv(CONFIG_ENV)
def externalDataSource = System.getenv(DATASOURCE_ENV)

if (externalConfig && new File(externalConfig).isFile()) {
    grails.config.locations << "file:" + externalConfig
}
else {
    grails.config.locations << "file:" + defaultConfigFile
}

if (externalDataSource && new File(externalDataSource).isFile()) {
    grails.config.locations << "file:" + externalDataSource
}
else {
    grails.config.locations << "file:" + defaultDatasourceFile
}

我在configs文件夹中有一些默认文件,但在服务器中,正在使用的文件位于:

I have some default files in configs folder, but in the server the files that are being used reside in:

/home/someusername/configuration/

它看起来不像默认路径,并且没有环境变量指向该配置建议的路径。

which doesn't look like the default path, and there are no environment variables pointing to that path as the configuration suggests.

我也试着寻找一个链接到其他配置文件夹的configs文件夹,但没有找到任何东西。

I also tried to look for a configs folder linked to the other configuration folder, but didn't find anything.

我迷失在这里;如何将配置文件指定给Grails?

I'm lost here; how else can configuration files be specified to Grails?

编辑:
为了澄清事情,服务器正在运行并运行,我只想弄清楚它会从上面指定的路径中获取配置文件。

To clarify on things, the server is running and works, I just want to figure out how it's picking up the configuration files from the above specified path.

推荐答案

如果您在使用外部配置时遇到困难, b在Config.groovy中指定了外部配置

In case you are struggling with external configs and you have specifies external configs like so in Config.groovy

grails.config.locations = []

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

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

,您设置的环境是这样的

and your set environment is something like this

export GRAILS_OPTS="$GRAILS_OPTS -DAppName.config.location=dev-config/Config.groovy"
export GRAILS_OPTS="$GRAILS_OPTS -DAppName.datasource.config.location=dev-config/DataSource.groovy"

并且dev configs没有被拾取,那么您需要查看BuildConfig.groovy中的fork设置并关闭它们。

and dev configs are not being picked up, then you need to look into your fork settings in BuildConfig.groovy and turn them off like so

grails.project.fork = [
  test: false,
  run: false, 
]

通过这种方式,当应用程序分叉时,系统属性不会丢失。

This way, when application is forked, your system properties will not be lost.

这篇关于Grails外部配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆