简体   繁体   English

在war文件中的WEB-INF外部创建配置属性文件

[英]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应用程序,现在我需要处理配置文件中的动态参数,我已经完成了数据源的工作,最后我将其放置在datasource.groovy文件中的代码下方

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 : 并在grails-app / config内创建了一个config.properties文件,它的工作正常,但问题是一旦我生成了战争文件,该文件的属性文件位于WEB-INF / classs内,但是我需要该文件不在WEB-INF之外,eaxmple结构喜欢 :

myApp : myApp:

  • config.properties config.properties
  • WEB-INF WEB-INF
  • assets 资产
  • css CSS
  • js...etc JS ...等

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). 例如在Tomcat中,它将是$ CATALINA_HOME / conf / catalina.properties (或$ CATALINA_BASE / conf / catalina.properties,如果存在)中的'common.loader'属性。 (If you use something like tomcat7-maven-plugin its < configuration > tag has '< additionalClasspathDirs >' element where you can specify the additional class paths). (如果使用类似tomcat7-行家-插件其< 配置 >标记有“<additionalClasspathDirs>”元素,用户可以指定附加的类路径)。

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. 对于Grails <3.0-在配置文件中,您可以放置​​或取消注释以下代码。 put it at the end of config.groovy file so it will override pre define config properties: 将其放在config.groovy文件的末尾,这样它将覆盖预定义的配置属性:

// 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. 然后在home .grails文件夹中创建myapp-config.properties或更好的myapp-config.groovy格式文件(“ myapp”是您的应用程序名称),以便在开发环境中为每个Grails应用程序提供一个外部配置文件。 For production it needs to be in the Tomcat class path - so a good place is to put it in the tomcat lib folder. 对于生产而言,它必须位于Tomcat类路径中-所以一个好地方是将其放在tomcat lib文件夹中。

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: 假设您的应用名为myapp,并且将配置文件myapp-config.groovy放置在用户主目录下名为myapp的文件夹中:

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)
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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