简体   繁体   中英

Create configuration properties file outside WEB-INF in war file

I have create small grails app now i needs to work on dynamic parameters in configuration file, i have done for data source, i placed below code in datasource.groovy file at the end

grails.config.locations = ["classpath:config.properties"] 

and created one config.properties files inside grails-app/config,its working fine but problem is once i generate war file that properties file location is inside WEB-INF/classes,but i need that file outside of WEB-INF, eaxmple structure like :

myApp :

  • config.properties
  • WEB-INF
  • assets
  • css
  • js...etc

how can i solve this issues?

My favorite option: specify an additional class path in your app server and place your property file(s) is this path .

Eg in Tomcat it will be 'common.loader' property located at $CATALINA_HOME/conf/catalina.properties (or $CATALINA_BASE/conf/catalina.properties if exists). (If you use something like tomcat7-maven-plugin its < configuration > tag has '< additionalClasspathDirs >' element where you can specify the additional class paths).

See this blog for more discussion on this topic.

for Grails <3.0 - in the config file you can put or uncomment the following code. put it at the end of config.groovy file so it will override pre define config properties:

// keep it at the end of the file - so you can override with external config
grails.config.locations = [ "classpath:${appName}-config.properties",
                            "classpath:${appName}-config.groovy",
                            "file:${userHome}/.grails/${appName}-config.properties",
                            "file:${userHome}/.grails/${appName}-config.groovy"]

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

then create myapp-config.properties or even better, myapp-config.groovy format file in home .grails folder ("myapp" is you application name) so you can have an external configuration file for each Grails application in your development environment. For production it needs to be in the Tomcat class path - so a good place is to put it in the tomcat lib folder.

Presuming your app is named myapp, and you place a configuration file named myapp-config.groovy in a folder named myapp under your user home:

def loadExternalConfiguration() {
    def contextPath = grailsApplication.mainContext.servletContext.contextPath.substring(1)
    def userHome = System.properties['user.home']
    def userConfigDir = new File("${userHome}/${contextPath}")
    if (userConfigDir.exists()) {
        File configFile = new File("${userConfigDir}/${contextPath}-config.groovy")
        if (configFile.exists() && configFile.isFile()) {
            def configSlurper = new ConfigSlurper()
            def configPart = configSlurper.parse(configFile.toURI().toURL())
            grailsApplication.config.merge(configPart)
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM