Reputation: 1231
I'm having a really simple problem getting ConfigSlurper
to process my config
Groovy version 2.5.6
Went back to basics and tried this simple Groovy script:
ConfigSlurper slurper = new ConfigSlurper ()
slurper.parse ("""host='localhost' """)
println slurper.getProperty('host')
/* gives exception :
Caught: groovy.lang.MissingPropertyException: No such property: host for class: groovy.util.ConfigSlurper
groovy.lang.MissingPropertyException: No such property: host for class: groovy.util.ConfigSlurper
at scripts.testSSlurper.run(testSSlurper.groovy:7)
*/
Why doesn't this simple parse fail?
What am I doing wrong here? This is a blocker for the real code I've written parsing a file - which also seems to bind nothing into slurper.
Upvotes: 1
Views: 1018
Reputation: 42214
There is one misunderstanding in your code sample. Parsing a config script does not mutate the ConfigSlurper
object, but it returns a ConfigObject
instead. All you have to do is to capture the result of slurper.parse(script)
method and access host
key from the returned ConfigObject
instance.
ConfigSlurper slurper = new ConfigSlurper()
def config = slurper.parse(""" host = 'localhost' """)
println config.getProperty("host")
The output:
localhost
Upvotes: 4