grails /外部配置/ grails.config.locations - 绝对路径文件“不存在”? [英] grails / external configuration / grails.config.locations - absolute path file "Does not exist"?
问题描述
我试图使用Grails的内置机制来加载部署的WAR文件外部的外部配置文件(* .groovy和* .properties)。文档意味着这只是使用适当的 classpath:
或<$ c>设置 grails.config.locations
$ c> file:路径。
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}\n"
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
-
档案:c:\path_to_file\configurationFile.properties
-
c :\path_to_file\configurationFile.properties
classpath:configurationFile.properties
file:c:\path_to_file\configurationFile.properties
c:\path_to_file\configurationFile.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"]
}
$ b b
这样我就可以在项目根目录中放置一个文件,以便在开发时(使用文件:location)和服务器类路径中的文件部署为战争时自定义属性。 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屋!