grails/外部配置/grails.config.locations - 绝对路径文件“不存在"? [英] grails / external configuration / grails.config.locations - absolute path file "Does not exist"?

查看:11
本文介绍了grails/外部配置/grails.config.locations - 绝对路径文件“不存在"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to use Grails' built-in mechanism for loading external configuration files (*.groovy and *.properties) outside the deployed WAR file. The documentation implies this is just a case of setting grails.config.locations with the appropriate classpath: or file: paths.

我已经配置了 Config.groovy:

I've configured Config.groovy with:

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}
"
    grails.config.locations << pathToResource
}

但是这并没有奏效,错误消息表明文件不存在".但是,打印存储在 grails.config.locations 中的绝对路径表明确实如此.我尝试了一些组合:

However this hasn't worked, with error messages indicating the file "Does not exist". However, printing the absolute path stored in grails.config.locations indicates it does. I have tried some combinations:

  • classpath:configurationFile.properties
  • file:c:path_to_fileconfigurationFile.properties
  • c:path_to_fileconfigurationFile.properties

但在所有这些情况下都找不到文件.

but in all these cases the file can't be found.

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

Very strange - advise appreciated. Or suggestions on how to debug.

推荐答案

我通常这样做:

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"]
}

这让我可以在项目根目录中放置一个文件,以便在开发时(使用文件:位置)在本地自定义属性,并在部署为 war 时在服务器的类路径中放置一个文件.Tomcat 的 lib 文件夹位于其类路径中,因此如果您使用 Tomcat,这是放置文件的好地方.通过将应用名称放在文件中,您可以拥有多个配置文件而不会相互干扰.

This lets me put a file in the project root to customize properties locally when developing (using the file: location) and a file in the server's classpath when deployed as a war. Tomcat's lib folder is in its classpath so that's a good place to put files if you're using Tomcat. By putting the app name in the file you can have multiple config files without them stepping on each other.

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

Be sure to add the local config file to svn:ignore or .gitignore so you don't check it into source control. Each developer can then have their own settings (or just use the defaults) without affecting the others.

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

This is a great way to externalize database passwords and other production values. The app deployer (ideally not a developer) manages the file and its contents and this avoids checking in passwords into source control. Much better than using JNDI IMO.

这篇关于grails/外部配置/grails.config.locations - 绝对路径文件“不存在"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆