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()
}

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