grails war tomcat with external config file

拈花ヽ惹草 提交于 2019-12-25 02:03:36

问题


I want to add external config data. So I added in the main Config.groovy file the following line.

I exported the environment variable

DASH_CONFIG=<path_to_external_configfile>/extConfig.properties
  • extConfig.properties

    simpleBSMDash.default.serverName = "test.it.com"
    simpleBSMDash.default.refreshInterval = "5"
    
  • Config.groovy

    if(System.getenv('DASH_CONFIG')) {
        println( "Including configuration file: " + System.getenv('DASH_CONFIG'));
        grails.config.locations = [ 
            "classpath:exthConfig.properties",
            "file:./extConfig.properties",
            "file:${System.getenv('DASH_CONFIG')}"]
    } else {
        println "No external configuration file defined."
    }
    
  • Bootstrap.groovy

    I'm initialisation some config parameter in Boostrap.groovy. for example

    import org.codehaus.groovy.grails.commons.ConfigurationHolder
    
    
    new AdminSettings(
        refreshInterval:"${ConfigurationHolder.config.simpleBSMDash.default.refreshInterval}", serverName:"${ConfigurationHolder.config.simpleBSMDash.default.serverName}").save(failOnError:true)
    

When running locally the app through: grails -Denv=DEV run-app. Everything works fine, the intance AdminSettings is created and initialised with the expected values.

When I deployed the war file on my tomcat server, the intance AdminSettings is not intantiated. Yet I did export environment variable

DASH_CONFIG=<path_to_external_configfile>/extConfig.properties.

I event put the extConfig.properties file in $TOMCAT_HOME/lib folder, it did not work as well.

I rename it to extConfig.groovy, it did not help either.

I search through stackflow but did not find out a correct answer.

Does anyone have an idea?


回答1:


Here's what works for us:

In conf/Config.groovy:

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

It works in Windows, though the places Groovy/Windows thinks is home (~) can be a bit surprising. Your attempt to access an environment variable from Config.groovy looks a bit risky...



来源:https://stackoverflow.com/questions/28437929/grails-war-tomcat-with-external-config-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!