Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
609 views
in Technique[技术] by (71.8m points)

grails / external configuration / grails.config.locations - absolute path file "Does not exist"?

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.

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
}

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is what I usually do:

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

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.

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...