Sunday, February 14, 2010

Accessing Grails application properties (metadata)

Accessing Grails application properties (metadata)

A Grails application.properties file contains meta type information about your grails application. This file is user editable and should only be used for declaring metadata that describes your application. Do not store configuration information in this file.

There are two ways to access the metadata contained within this file.

Accessing from GSP:

There is a GSP tag within the standard Grails tag library that allows access to this metadata, and surprise surprise its called g:meta.
<g:meta name="app.grails.version"/>
The above tag will render the version of the Grails Framework that is expected by your application.

Accessing from application code:

There is a very useful class within the Grails commons package called GrailsApplication. An instance of this class is automatically wired into Grails controllers and can be referenced from the following variable.
grailsApplication
If you wish to access the GrailsApplication instance outside of a controller you will need to do as follows.
import org.codehaus.groovy.grails.commons.ApplicationHolder

...

def grailsApplication = ApplicationHolder.application
Once you have access to the GrailsApplication instance you can then reference the application metadata as follows.
//Print application version
println grailsApplication.metadata.'app.version'

//Print application name
println grailsApplication.metadata.'app.name'

//Print expected Grails version
println grailsApplication.metadata.'app.grails.version'

2 comments:

Anonymous said...

Thank you! Helpful right when I needed it.

Lead Visionary said...

Good show, this was exactly what I was looking for

Post a Comment