从环境变量将grails配置外部化为多个属性文件

从环境变量将grails配置外部化为多个属性文件,grails,configuration,externalizing,Grails,Configuration,Externalizing,我设置了一个环境变量,如下所示: 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(

我设置了一个环境变量,如下所示:

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.locations
(复数)。我(非常有限)的经验表明,在
Config.groovy
完成之前,可能不会加载外部文件

您可能想考虑在类路径上寻找附加的配置文件;然后,您可以将额外的内容放在Grails项目之外(例如,在web服务器的库中)或

Grails app/conf
目录中。我已经写了如何做的说明


下面是一篇关于如何从插件中执行此操作的帖子:

感谢您的提示,我将尝试一下,另一件事是grails有一个预定义的设置,可以从../${appName}-config.properties读取与datasource.groovy相关的属性(就像我使用密码编解码器,所以我使用codec.decode(“加密密码”)在datasource中,如何在属性文件中执行此操作?),甚至标签都是由grails定义的,如datasource.username、datasource.drivercassname等。如何自定义或添加一些在run app中读取并在datasource.groovy中使用的属性?如果您查看原始
Config.grooy
文件的顶部,您将看到,它在评论中对如何做到这一点提出了一些建议。您可能需要将文件编码为
scripts/Events.groovy
,就像我在链接到的帖子中所做的那样。我不知道dataSource.groovy问题的答案;我只是用自己的替换默认的
DataSource.groovy
。我认为,在部署应用程序时,您可能希望使用JNDI数据源,而不需要在war文件中存储密码。
    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...