Thursday, 15 October 2009

Determine Base-Directory from Running Grails Application

It's been a while since I've had chance to post, so here are a couple of short articles to get back into the swing of it.

First off, here's how to determine the base directory of a running Grails application:

String dir = grailsAttributes.getApplicationContext().getResource("/").getFile()

The grailsAttributes object is available in all Controllers, but is not directly available in Services (nor can you inject it).

Should you need to to determine the base directory from within a Service (or access grailsAttributes etc) you need implement the ApplicationContextAware interface:

import org.springframework.context.*

class MyService implements ApplicationContextAware {
ApplicationContext applicationContext

...

String dir = applicationContent.getResource("/").getFile()
}

1 comments:

  1. Nice... was helpful to find out where grailsAttribute came from as I never saw this mentioned anywhere. Was nice to see the documentation on this.

    ReplyDelete