【问题标题】:Grails external configuration fileGrails 外部配置文件
【发布时间】:2014-03-14 13:35:18
【问题描述】:

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

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 文件夹中有一些默认文件,但在服务器中,正在使用的文件位于:

/home/someusername/configuration/

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

我还尝试查找链接到其他配置文件夹的配置文件夹,但没有找到任何内容。

我迷路了;配置文件还能如何指定给 Grails?

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

【问题讨论】:

  • 你确定这些环境变量没有被传递到服务器上的相关进程(Tomcat?)即使它们没有在你检查的任何shell中设置?通常当我需要设置这样的东西时,我会将环境设置放入 Tomcat 启动脚本中。
  • 这就是我想念的伊恩,他们被设置在脚本中。谢谢!

标签: grails


【解决方案1】:

我认为您的意图是使用 grails.config.locations[] 条目。以下在我的 Config.groovy 中被注释掉了

// grails.config.locations = [ "classpath:${appName}-config.properties",    
//                             "classpath:${appName}-config.groovy",
//                             "file:${userHome}/.grails/${appName}-config.properties",
//                             "file:${userHome}/.grails/${appName}-config.groovy"]

所以你不能指定这样的东西吗:

grails.config.locations = [ "file:${userHome}/configuration" ] 

要提取该文件夹中的文件吗?

【讨论】:

  • 对,但当前配置文件的处理方式并非如此。查看我更新的帖子。
【解决方案2】:
if(File.separator == "\\") {
    defaultConfigFile = defaultConfigFile.replace('/', '\\')
    defaultDatasourceFile = defaultDatasourceFile.replace('/', '\\')
}

这可能会导致问题 - grails.config.locationsURLs 列表,而不是本地文件路径,因此您肯定需要正斜杠,而不是反斜杠。而是尝试类似

File defaultConfigFile = new File(basedir, "configs/BombayConfig.groovy")
// and the same for datasource

// ...
File externalConfig = System.getenv(CONFIG_ENV) ? new File(System.getenv(CONFIG_ENV)) : null

if (externalConfig?.isFile()) {
    grails.config.locations << externalConfig.toURI().toString()
}
else {
    grails.config.locations << defaultConfigFile.toURI().toString()
}

【讨论】:

    【解决方案3】:

    如果您正在为外部配置而苦苦挣扎,而您 已经在 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"]
    }
    

    你设定的环境是这样的

    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"
    

    并且没有获取开发配置,那么您需要查看 BuildConfig.groovy 中的 fork 设置并像这样关闭它们

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

    这样,当应用程序被分叉时,您的系统属性不会丢失。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-01
      • 2012-12-20
      • 1970-01-01
      • 1970-01-01
      • 2014-08-23
      相关资源
      最近更新 更多