Grails reading from the filesystem

This might be trivial, but I search for it again and again, and now I’ve decided to write it down, for myself to remember it (and for others to find).

I sometimes find myself in need of reading data from my Grails projects filesystem, most of the times when bootstrapping the application.

Inject ‘grailsApplication’ in your BoosStrap, Controllers, TagLibs and Services: 

def grailsApplication

Place your files in /web-app/WEB-INF/resources and use this line of code to retrieve the directory of your resources:

def resources = grailsApplication.mainContext.getResource('/WEB-INF/resources').file
 
This works both in development and production
 
Blog comments powered by Disqus