Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access config file kept in WEB-INF folder

I am new to groovy. and looking for a way to store common configs at one place so my application can access it.

I am keeping configurations for my groovy applications in WEB-INF, how can I access the file from my main application?

I tried this ConfigSlurper().parse(new File("config.groovy").toURL()), but can't get it working, because the path where tomcat is expecting the config.groovy is where tomcat is installed. I have my appBase configured to some other location.

How do I give relative path to new File('') so that it can read the config file?

like image 460
Salman Avatar asked Dec 29 '25 01:12

Salman


1 Answers

You want to do this using the ClassLoader and not the filesystem directly. Basically, don't use File. I'm not sure how to do this in Groovy, but in plain-old Java it would look like this:

URL config = request.getServletContext().getResource("/WEB-INF/config.groovy");

... then do whatever you want with the URL. (FYI the getResource method uses the ClassLoader of the webapp). So, maybe in Groovy that might be:

ConfigSlurper().parse(request.ServletContext.getResource("/WEB-INF/config.groovy"))

Sorry I can't give you the exact Groovy syntax... not my world. :)

like image 191
Christopher Schultz Avatar answered Dec 30 '25 22:12

Christopher Schultz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!