Honestly, I'm not sure if the groovy template engine supports a way to ignore parameters; (leaving the placeholder as it is when the corresponding parameter is missing), but here's the hack.
import groovy.text.*;
def template = "\${PARAM1} \${PARAM2} \${PARAM3} \${PARAM4} \${PARAM5} \${PARAM6}"
//example hard coded params; you can build this map dynamically at run time
def parameters = [
"PARAM1": "VALUE1",
"PARAM2": "VALUE2",
"PARAM3": null,
"PARAM4": "VALUE4",
"PARAM5": null,
"PARAM6": "VALUE6"
]
//this is the hack
parameters.each{ k, v ->
if(!v){
parameters[k] = "\$$k"
}
}
def templateEngine = new SimpleTemplateEngine()
def output = templateEngine.createTemplate(template).make(parameters)
print output
Output:
VALUE1 VALUE2 $PARAM3 VALUE4 $PARAM5 VALUE6
source
share