【问题标题】:externalizing grails config into multiple properties files from environment variable将 grails 配置从环境变量外部化到多个属性文件中
【发布时间】:2012-03-24 22:34:18
【问题描述】:

我已经设置了一个这样的环境变量:

APP_HOME = "c:\app\app-datasource.properties

在 config.groovy 我做

def ENV_NAME = "APP_HOME"
    if(!grails.config.location || !(grails.config.location instanceof List)) {
    grails.config.location = []
    }
    if(System.getenv(ENV_NAME)) {
    println "Including configuration file specified in environment: " + System.getenv(ENV_NAME);
    grails.config.location << "file:" + System.getenv(ENV_NAME)

    } else if(System.getProperty(ENV_NAME)) {
    println "Including configuration file specified on command line: " + System.getProperty(ENV_NAME);
    grails.config.location << "file:" + System.getProperty(ENV_NAME)

    } else {
    println "No external configuration file defined."
    }

我从网上的帖子中得到这个,我想知道我们是否需要使用 grails.config.locationgrails.config.locations ? 另外,而不是将APP_HOME 直接设置为属性文件,我可以将其设置为目录路径(e.g.: c:\apps),然后我可以在该目录中放置多个属性文件,那么如果我多次执行以下操作会起作用吗? :

    grails.config.locations << "file:" + System.getProperty(ENV_NAME)+ "\app-datasource.properties"
    grails.config.locations << "file:" + System.getProperty(ENV_NAME)+ "\app-reporting.properties"
and so on...

提前致谢

【问题讨论】:

    标签: grails configuration externalizing


    【解决方案1】:

    您需要修改grails.config.locations(复数)。我的(非常有限的)经验表明,外部文件可能要等到Config.groovy 完成后才会加载。

    您可能需要考虑在您的类路径中查找其他配置文件;然后您可以将您的额外内容放在 Grails 项目之外(例如,在您的 Web 服务器的库中)或 grails-app/conf 目录中。我已经写了关于如何做到这一点的说明here

    这里有一篇关于如何通过插件做到这一点的帖子:https://stackoverflow.com/a/9789506/1269312

    【讨论】:

    • 感谢 Ed 的提示,我要试试这个,另一件事是 grails 有一个预定义的设置,可以从 ../${appName}-config.properties 读取与数据源相关的属性。 groovy (就像我使用编解码器作为密码,所以我在数据源中使用 codec.decode("encrypted passwd") ,我如何在属性文件中执行此操作?),甚至标签都是由像 dataSource.username、dataSource 这样的 grails 定义的.driverClassName 等。如何自定义或添加更多在 run-app 中读取并在 dataSource.groovy 中使用的属性?
    • 如果您查看原始Config.grooy 文件的顶部,您会发现它在 cmets 中有一些关于如何执行此操作的建议。您可能必须将您的文件编码为scripts/Events.groovy,就像我在链接到的帖子中所做的那样。我不知道 dataSource.groovy 问题的答案;我只是用我自己的替换默认的DataSource.groovy。我认为您在部署应用程序时会想要使用 JNDI 数据源,这不需要您在 war 文件中存储密码。
    猜你喜欢
    • 2020-03-06
    • 2017-10-29
    • 2017-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多