I've added the code below to my Config.groovy file, however, in spite of having it I'm not able to access the external configuration properties in the Config.groovy file.
Does anyone how I can access the properties of the external configuration file within the Config.groovy file?
if (System.getProperty("CONFIG")) {
grails.config.locations << "file:" + System.getProperty("CONFIG")
} else {
grails.config.locations << "file:./${appName}-config.properties"
}
Note: I've tried using ${...} like I would in Spring configuration files, ConfigurationHolder.config, and grailsApplication to access the properties but none of these approaches work.
Thanks.
I think this would cause confusion due to the order the config files are loaded. I'm pretty sure at the time the Config.groovy is loaded, the external one hasn't been loaded yet.
So your "CONFIG" property is set in the external file, which is the name of the file that you want to load?
How I usually do this is just list all the files that I could use.
grails.config.locations = [
"file:../app-config/myapp-dataSource.groovy",
"file:../app-config/myapp-config.groovy"
]
environments {
development {
grails.config.locations = [
"file:../myapp-config/myapp-dataSource.groovy",
"file:../myapp-config/myapp-config.groovy",
"file:${userHome}/myapp-config/myapp-dataSource.groovy",
"file:${userHome}/myapp-config/myapp-config.groovy"
]
}
...
}
If the files do not exist they are just skipped. Files I believe are loaded in order, so anything in the ${userHome} dir, will override the previously set values. This is nice for development, as you can have machine local ways of changing settings, and not have to worry about these config changes being checked in.
#Nick Larson,
What you mentioned about CONFIG not loaded is not true. If CONFIG is a JVM parameter, set with -DCONFIG=xxxx, then it is set before config.groovy kicks in.
#Kin1,
You are using file: protocol for accessing the property file. Are you trying to access this in a WAR or EAR file or is it a file based system.
In a WAR or EAR file you need to use classpath: for the file, file: does not work. Also, you have to make sure to actually copy the Groovy file(not compiled class file) in the classpath. We do it on WAR create event and the build process copy the config file into one of the classpath location.
Hope this helps.
Add the below line in config.groovy
grails.config.locations = [ "classpath:grails-app-config.properties"]
environments {
development {
grails.logging.jul.usebridge = true
grails.config.locations = ["file:C:\\conf\\externalfile.groovy"]
}
production {
grails.logging.jul.usebridge = false
grails.config.locations = ["file:/opt/config/externalfile.groovy"]
// TODO: grails.serverURL = "http://www.changeme.com"
}
}
If you want to access any property from external configuration(config.groovy) then just declare the property like
property = property value eg:(ImagePath = "C:\\Users\\Saved Pictures")
access it like grailsApplication.config."property"
eg:(grailsApplication.config.ImagePath)
NOTE: dont use def just a property and its value.
Related
At the top of config.groovy is this line (now uncommented):
grails.config.locations = [ "file:${userHome}/.grails/${appName}-config.properties"]
So we created a properties file on windows in our home dir /.grails, which contains these lines for the database migration plugin:
contents of c:\Users\me\.grails\myapp-config.properties:
grails.plugin.databasemigration.updateOnStartContexts=XXX
grails.plugin.databasemigration.updateOnStart=true
we restart the app, it completely ignores these values.
If however, we change to a groovy file thusly:
grails.config.locations = [ "file:${userHome}/.grails/${appName}-config.groovy"]
And create the following file: c:\Users\me.grails\myapp-config.groovy
grails.plugin.databasemigration.updateOnStartContexts = 'XXX'
grails.plugin.databasemigration.updateOnStart = true
It works perfectly. It seems it is not possible to use property files with the database migration plugin at least.
However, we need to be able to change these values on production (tomcat + war), which we assume cannot compile groovy files, so wont work.
Luckily, the below part does actually work:
if (System.properties["${appName}.config.location"]) {
grails.config.locations << "file:" + System.properties["${appName}.config.location"]
}
But we would really like to get the home dir version working, so we dont have to worry about system properties and command line arguments in our dev environments.
Any ideas?
change the path separator for windows from / to \
try to put the complete path in locations to check the file can be read
I want to separate config files into few small parts. In Config.groovy I have defined grails.config.locations array to point these files:
grails.config.locations = [
"classpath:config.properties",
"classpath:some-config.groovy",
]
And then I am checking configuration map by accessing: grailsApplication.config
The first configuration file is Java properties file, which loads properly:
config.properties
grails.serverURL=http://localhost:8080/selly
..
The second one is .groovy file which in reference to the documentation (http://grails.org/doc/latest/guide/conf.html#configExternalized) should be loaded from automatically parsed ConfigSlurper file format:
some-config.groovy:
app {
testvar {
foo = true
}
}
But grailsApplication.config.app does not exists (no field in debug and println returns empty map [:]).
Can anyone give an example of loading groovy files?
Files are placed in: grails-app\conf\, for example grails-app\conf\config.properties
It looks like you've configured both files correctly. grailsApplication.config.app might be null simply because it is not a leaf node, have you tried grailsApplication.config.app.testvar.foo?
As there is sensitive code in the config.groovy file, I am afraid that my friends will commit with bugs in this file. When getting svn update, we too will get the buggy config code.
Can i split the code at config.groovy in such a way that the sensitive code remains untouched and the other can be changed frequently?
Inside your main config file you are able to access this variable:
grails.config.locations
It is a list of configuration file locations to which you can add your own files:
grails.config.locations << 'file:MyConfigFile.groovy'
These files will then be added to your configuration.
For a more elaborate setup see this blog post:
http://www.pasopas.nl/2012/loading-grails-configuration-files-update/
Similar to Marijn's answer. This is how I usually set up my Config.groovy. I still use it for some settings, but anything environmentally (deployment location or individual machine) changing can override any settings in the Config.groovy.
Config.groovy >>>>
grails.config.locations = [
"file:../app-config/myapp-dataSource.groovy",
"file:../app-config/myapp-config.groovy"
]
environments {
development {
grails.config.locations = [
"file:../myapp-config/myapp-dataSource.groovy",
"file:../myapp-config/myapp-config.groovy",
"file:${userHome}/myapp-config/myapp-dataSource.groovy",
"file:${userHome}/myapp-config/myapp-config.groovy"
]
some.config.setting=true
}
}
file:${userHome}/myapp-config/myapp-config.groovy >>>>
some.config.setting=false
Grails 1.3.7
I have some configuration located in an external config file. One of the entires looks like this:
site.maintenance.mode = false
I have a filter which checks for certain config settings for specific URLs. When I do a run-app or deploy a WAR into Tomcat and do:
boolean maintenanceMode = grailsApplication.config.site.maintenance.mode
maintenanceMode is coming back true. If I look at the config object in debug mode, this is what I get:
site={maintenance={mode=false, message="<p>Our trail guides are working hard to get the system back on track.</p><p>We're sorry, the account system is down for maintenance at the moment. We'll get it back online as quickly as we can. Thanks for your patience.</p>"}}
I have a controller that I use to reload this config file dynamically and hitting this controller will fix the issue. But I'm curious as to why it is incorrect on first runs and why the discrepency in what is getting put in the maintenanceMode variable vs what is actually in the config object.
Are you using a Java properties file or a Groovy file? If you're using a properties file then I believe Grails will interpret site.maintenance.mode=false the same way as site.maintenance.mode='false' and since Groovy will interpret:
"false".asBoolean() == true
then that would explain why you would see that initial true value.
I just ran a simple test locally to verify this behavior. When I externalize my properties in a file called test.properties then site.maintenance.mode=false initially gets a boolean value of true, when I use a file called test.groovy then it interprets the boolean value of site.maintenance.mode=false as false. I believe this is because when you use a Groovy file Grails uses ConfigurationSlurper to process it but when you use a properties file Grails interprets everything as String name/value pairs.
What I do is to have an external Config.groovy file, for instance: MyConfig.groovy
At the end of the standard grails Config.groovy file, I have the following:
def ENV_NAME = "MY_EXTERNAL_CONFIG"
if(!grails.config.locations || !(grails.config.locations instanceof List)) {
grails.config.locations = []
}
if(System.getenv(ENV_NAME)) {
grails.config.locations << "file:" + System.getenv(ENV_NAME)
} else if(System.getProperty(ENV_NAME)) {
grails.config.locations << "file:" + System.getProperty(ENV_NAME)
} else {
println "No external Configs found."
}
So now you can have a MyConfig.groovy file anywhere in production environment (for example) and then set an Environment system variable to point to this file (or pass it as parameter to startup.sh), before you start tomcat:
MY_EXTERNAL_CONFIG="/home/tomcat/configs/MyConfig.groovy"
export MY_EXTERNAL_CONFIG
That's it. Now you have an external MyConfig.groovy file. The properties in it are accessible from your grails app as they were part of the standard Config.groovy
import org.codehaus.groovy.grails.commons.*
//...
ConfigurationHolder.config.foo.bar.hello
Hypothetical situation:
I have downloaded a Grails application from the web as a WAR file, foo.war. In the documentation it says that I can put my own custom configuration in /foo.groovy, because this path is included in grails.config.locations in Config.groovy. I can dump all my custom config in that one file and life is good.
How, here's my problem... The configuration for FooApp is big and hairy, and I don't want it all in one file. I would like to break it up into /bar.groovy and /baz.groovy to keep things organized. Is there a way to specify something in /foo.groovy so that FooApp will also pick up /bar.groovy and /baz.groovy and process them?
I already tried appending paths to grails.config.locations in /foo.groovy, but Grails didn't like that and threw a nasty exception on startup. I'm not sure what other approach to take.
Edit for clarity:
grails-app/conf/Config.groovy looks like this:
grails.config.locations = ["file:/foo.groovy"]
Now, without modifying grails-app/conf/Config.groovy, and only by modifying /foo.groovy, is there a way to load more config files other than /foo.groovy?
You could slurp the additional config files within foo.groovy:
foo.groovy
port {
to {
somewhere=8080
another {
place=7070
}
}
}
host = new ConfigSlurper().parse(new File("bar.groovy").toURL())
bar.groovy
to {
somewhere="http://localhost/"
another {
place="https://another.place.com/"
}
}
So within your app you have:
assert grailsApplication.config.port.to.somewhere == 8080
assert grailsApplication.config.port.to.another.place == 7070
assert grailsApplication.config.host.to.somewhere == "http://localhost/"
assert grailsApplication.config.host.to.another.place == "https://another.place.com/"