Stefan Ciprian Iuga
Stefan Ciprian Iuga

Reputation: 35

ConfigSlurper().parse() not working

It seems that for whatever reason my ConfigSlurper().parse is not working properly. Whenever I pass it any configuration if is either hardcoded or in a config.groovy file it creates an empty map. this is what I have:

    def parser = '''foo:{
        path: "path"
        hidden: true
        version: Framework_V4_0
        host: localhost:8080
            }'''

    ConfigObject conf = new ConfigSlurper().parse(new File("src/config.groovy").toURL())
    println new File("src/config.groovy").toURL()
    println conf.app

and the config.groovy file:

app {
    path: "path"
    hidden: true
    version: "Framework_V4_0"
    host: "localhost:8080"
}

Upvotes: 2

Views: 1475

Answers (1)

m0skit0
m0skit0

Reputation: 25873

That syntax is incorrect. As explained in the ConfigSlurper documentation, it expects configuration files defined in the form of Groovy scripts.

def parser = '''app {
    path = 'path'
    hidden = 'true'
    version = 'Framework_V4_0'
    host = 'localhost:8080'
}'''

ConfigObject conf = new ConfigSlurper().parse(parser)
println conf

PS: File#toURL() is deprecated, don't use it.


Reading it from a file in Groovy is straight-forward:

def parser = '''app {
    path = 'path'
    hidden = 'true'
    version = 'Framework_V4_0'
    host = 'localhost:8080'
}'''

def file = new File('config')
file << parser
ConfigObject conf = new ConfigSlurper().parse(file.text)
println conf

Upvotes: 3

Related Questions