Can I use ConfigSlurper to refer to the configuration file in the classpath?

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

+4
source share
3 answers

I assume that it happens that your Groovy file compiles and ends up being a class in your binary directory (classpath). Instead of loading it at the url, try loading the script class.

Class scriptClass = getClass().classLoader.loadClass('com.corp.MyConfig') ConfigObject config = new ConfigSlurper().parse(scriptClass) 
+2
source

If your configuration file is accessible in 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); 
+2
source

From POGO you can also use:

 import grails.util.Holders class Foo { def bar() { println(Holders.config.grails.serverURL) } } 

From: How do I get to the goodies in my Grails Config.groovy at runtime?

0
source

All Articles