I need a custom command-line task in my grails app, so I have created a script using grails create-script my-script.
From that script, I would like to access some of the application’s config properties. Normally you can do this via grailsApplication.config. However, it seems grailsApplication is not available from within the context of a command-line script. The following script….
includeTargets << grailsScript("Init")
target(main: "The description of the script goes here!") {
println grailsApplication.config.mysetting
}
setDefaultTarget(main)
…yields (when run as grails my-script):
groovy.lang.MissingPropertyException: No such property:
grailsApplication for class: MyScript
I also tried ConfigurationHolder.config, which just returns null.
So how can I access application config from a script launched from the command-line?
The trick is to pull in the boostrap targets and depend on them, as shown below. Note that the application context object is called
grailsApp, and notgrailsApplicationat this stage.You might also be able to use the run-script command. This didn’t work for me, because it tries to initialise a Hibernate session. My app uses mongodb as a primary datastore, which requires that you uninstall the Hibernate plugin – so run-script fails.