Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use ConfigSlurper to reference a config file in the classpath?

Tags:

grails

I want to use the contents of a config file in several ways, including in integration tests and in my BootStrap. If my config file is under src/groovy and is called "com.corp.MyConfig.groovy", what should I pass to the ConfigSlurper parse method?

like image 800
Jim Norman Avatar asked May 25 '11 15:05

Jim Norman


2 Answers

I guess what happens is that your Groovy file gets compiled and ends up being a class in your binary directory (classpath). Instead of trying to load it via the URL try to load the script class.

Class scriptClass = getClass().classLoader.loadClass('com.corp.MyConfig')
ConfigObject config = new ConfigSlurper().parse(scriptClass)
like image 135
Benjamin Muschko Avatar answered Sep 30 '22 04:09

Benjamin Muschko


If your config file is available on the classpath, I would suggest using ClassLoader.getResource() to get it:

URL url = MyClass.class.getClassLoader().getResource("com/corp/MyConfig.groovy");
config = new ConfigSlurper().parse(url);
like image 40
dogbane Avatar answered Sep 30 '22 04:09

dogbane