2017-04-26 89 views
0

我想在使用不同數據庫的同一個tomcat中同時運行不同版本的Grails(2.3.5)應用程序。特定於戰爭的配置文件

此刻我有一個外部配置文件,我指定了我的數據庫配置。

的文件名是硬編碼:

<tomcat-home>/conf/sc.conf 

「sc.war」 是我的戰爭文件的名稱。因爲我現在想要/需要運行多個版本,我想創建conf文件變量的名稱。

<tomcat-home>/conf/<warfilename>.conf 

有沒有辦法在運行時獲取war-file-name?

我打開其他方式來解決我的問題。謝謝你的幫助。

回答

0

你只需要給applicationBaseDir路徑到外部配置文件

applicationBaseDir    =  "" 
println "Bootstrapping application" 
println ">>>>>>>>Environment >>>>>"+GrailsUtil.environment 

Environment.executeForCurrentEnvironment { 
    development { 
     println "Keeping applicationBaseDir "+grailsApplication.config.applicationBaseDir+" as is." 
    } 
    production { 
//Following code finds your project path and set applicationBaseDir 
     Process findPresentWorkingDirectory = "pwd".execute() 
     def pwd = findPresentWorkingDirectory.text 
     def pwdTokens = pwd.tokenize("/") 
     pwdTokens.pop() 
     applicationBaseDir = pwdTokens.join("/") 
     println ">>>> applicationBaseDir "+applicationBaseDir 
     applicationBaseDir = "/"+applicationBaseDir+"/webapps/applicationDir/" 
     println 'grailsApplication.config.applicationBaseDir '+applicationBaseDir 
    } 

}

或者你在外部配置文件中直接設置applicationBaseDir

applicationBaseDir = "/home/tomcat/webapps/applicationDir/" 

現在,是指特定的外部配置文件添加到您的項目文件Config.groovy文件以下代碼

environments { 
    development { 

    String codeSystemClassFilePath = AnyDomainClassNameFromYourApplication.class.getProtectionDomain().getCodeSource().getLocation().getPath() 
// Here AnyDomainClassNameFromYourApplication change to your application domain class name e.g.I have domain class Student then I replace it with Student 
    def arrayWithLastValueAsProjectName = codeSystemClassFilePath.split('/target/classes/')[0].split('/') 
    String projectName     = arrayWithLastValueAsProjectName[arrayWithLastValueAsProjectName.size()-1] 
    println "App name in dev==>"+projectName 


    grails.logging.jul.usebridge = true 
    grails.config.locations = [ 
      "file:${userHome}/grails/${projectName}.groovy" 
    ] 

    grails.assets.storagePath = "" 
    } 
    production { 

    String codeSystemClassFilePath = AnyDomainClassNameFromYourApplication.class.getProtectionDomain().getCodeSource().getLocation().getPath() 
    def arrayWithLastValueAsProjectName = codeSystemClassFilePath.split('/WEB-INF/classes/')[0].split('/') 
    String projectName     = arrayWithLastValueAsProjectName[arrayWithLastValueAsProjectName.size()-1] 
    println "App name in production==>"+projectName 

    grails.logging.jul.usebridge = false 
    grails.config.locations = [ 
      "file:${userHome}/grails/${projectName}.groovy" 
    ] 
} 
} 
//here grails/ is my default folder use to store exernal-config-files 

我希望這可以幫助你!

+0

感謝您的回答。我希望我的配置文件依賴於戰爭文件名稱。一些例子: abc.war - > /conf/config-abc.groovy或 yxz.war - > /conf/config-xyz.groovy – user3675091

+0

我編輯了答案,因此您可以從應用程序中引用外部配置文件。 –

+0

謝謝,看起來不錯。會嘗試讓你知道。 – user3675091