grails/external configuration/grails.config.locations - 绝对路径文件"不存在"?

Ale*_*lex 7 configuration grails external

我正在尝试使用Grails的内置机制在部署的WAR文件之外加载外部配置文件(*.groovy和*.properties).文档暗示这只是设置的情况下grails.config.locations用适当的classpath:file:路径.

我已将Config.groovy配置为:

String externalConfigLocation = System.getProperty("SYSTEM_PROPERTY_KEY")
if (!grails.config.locations || !(grails.config.locations instanceof List)) {
    grails.config.locations = []    
}
if (classpathExternalConfigLocation) {
    String pathToResource = "\"file:${basedir}" + File.separator + externalConfigLocation+"\""

    print "Loading external configuration file: ${pathToResource}\n"
    grails.config.locations << pathToResource
}
Run Code Online (Sandbox Code Playgroud)

但是这没有用,错误消息指示文件"不存在".但是,打印存储的绝对路径grails.config.locations表示它确实存在.我尝试了一些组合:

  • classpath:configurationFile.properties
  • file:c:\path_to_file\configurationFile.properties
  • c:\path_to_file\configurationFile.properties

但在所有这些情况下都无法找到该文件.

很奇怪 - 建议赞赏.或者有关如何调试的建议.

Bur*_*ith 13

这就是我通常做的事情:

grails.config.locations = ["classpath:${appName}-config.groovy",
                           "file:./${appName}-config.groovy"]
if (System.properties["${appName}.config.location"]) {
   grails.config.locations << "file:" + System.properties["${appName}.config.location"]
}
Run Code Online (Sandbox Code Playgroud)

这允许我在项目根目录中放置一个文件,以便在开发(使用文件:location)和服务器类路径中的文件作为战争时在本地自定义属性.Tomcat的lib文件夹位于其类路径中,因此如果您使用Tomcat,这是放置文件的好地方.通过将应用程序名称放在文件中,您可以拥有多个配置文件,而不会相互踩踏.

请务必将本地配置文件添加到svn:ignore或.gitignore,这样就不会将其检入源代码管理中.然后,每个开发人员都可以拥有自己的设置(或只使用默认设置),而不会影响其他开发人员.

这是外部化数据库密码和其他生产值的好方法.应用程序部署者(理想情况下不是开发人员)管理文件及其内容,这避免了将密码签入源代码管理.比使用JNDI IMO好多了.