簡體   English   中英

在war文件中的WEB-INF外部創建配置屬性文件

[英]Create configuration properties file outside WEB-INF in war file

我已經創建了一個小型grails應用程序,現在我需要處理配置文件中的動態參數,我已經完成了數據源的工作,最后我將其放置在datasource.groovy文件中的代碼下方

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

並在grails-app / config內創建了一個config.properties文件,它的工作正常,但問題是一旦我生成了戰爭文件,該文件的屬性文件位於WEB-INF / classs內,但是我需要該文件不在WEB-INF之外,eaxmple結構喜歡 :

myApp:

  • config.properties
  • WEB-INF
  • 資產
  • CSS
  • JS ...等

我該如何解決這個問題?

我最喜歡的選項:在應用服務器中指定其他類路徑,並將屬性文件放置在此路徑中。

例如在Tomcat中,它將是$ CATALINA_HOME / conf / catalina.properties (或$ CATALINA_BASE / conf / catalina.properties,如果存在)中的'common.loader'屬性。 (如果使用類似tomcat7-行家-插件其< 配置 >標記有“<additionalClasspathDirs>”元素,用戶可以指定附加的類路徑)。

有關此主題的更多討論,請參見此博客

對於Grails <3.0-在配置文件中,您可以放置​​或取消注釋以下代碼。 將其放在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"]
}

然后在home .grails文件夾中創建myapp-config.properties或更好的myapp-config.groovy格式文件(“ myapp”是您的應用程序名稱),以便在開發環境中為每個Grails應用程序提供一個外部配置文件。 對於生產而言,它必須位於Tomcat類路徑中-所以一個好地方是將其放在tomcat lib文件夾中。

假設您的應用名為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