2017-04-26 90 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