AMAN KUMAR
AMAN KUMAR

Reputation: 277

Reading Properties file in groovy

I am trying to put dynamic value in the argument of ConfigSlurper(String env) but it is displaying no value, it displays value only when I pass the string constant as env in ConfigSlurper.

Ex: 
def config = new ConfigSlurper('PRODUCT').parse(propertiesFile)
println "PRODUCT_NAME: "+config.PRODUCT_NAME

o/p:

PRODUCT_NAME: TYPEB_Routing,TYPEB_Mediation,TYPEX_Routing,TYPEX_Mediation

But when I try to read property file by using dynamic value in env I'm getting:

Ex: 
def pdt1 = "PRODUCT"
def config = new ConfigSlurper('$pdt1').parse(propertiesFile)
println "PRODUCT_NAME: "+config.PRODUCT_NAME

o/p:

 PRODUCT_NAME: [:]

Why this is happening, I'm not getting?? Please Explain....

Upvotes: 1

Views: 5091

Answers (2)

Opal
Opal

Reputation: 84756

'$pdt1' definitely won't work ' does not evaluate GString so the value passed will be $pdt1 literally. You need to use " - double quotes.

Maybe you also need to pass "$pdt1".toString() - convert the argument to String explicitly.

Upvotes: 0

Jason Plurad
Jason Plurad

Reputation: 6792

You should try using double quotes on the interpolated string, like this:

def config = new ConfigSlurper("${pdt1}").parse(propertiesFile)

Single quoted strings don't support interpolation.

Here's the documentation on the syntax. http://docs.groovy-lang.org/latest/html/documentation/index.html#_string_interpolation

Upvotes: 3

Related Questions