Templates 在groovy中解析属性文件,其中键包含点(.)并用作模板绑定
这在Java世界中非常常见,属性文件的键中有点(.)。Maven和Gradle很自然地解决了这个问题,但现在我需要在普通groovy中这样做:Templates 在groovy中解析属性文件,其中键包含点(.)并用作模板绑定,templates,groovy,Templates,Groovy,这在Java世界中非常常见,属性文件的键中有点(.)。Maven和Gradle很自然地解决了这个问题,但现在我需要在普通groovy中这样做: def propertiesFileContent = 'a.b=http://my.com/test' def templateContent = 'Testing ${a.b} ' def props = new Properties() props.load(new StringReader(propertiesFileContent)) def
def propertiesFileContent = 'a.b=http://my.com/test'
def templateContent = 'Testing ${a.b} '
def props = new Properties()
props.load(new StringReader(propertiesFileContent))
def template = new groovy.text.SimpleTemplateEngine().createTemplate(templateContent)
println template.make(props)
上面的代码引发异常,因为模板引擎威胁${a.b}作为“a”对象的“b”字段,并且不接受“a.b=”进行替换。
首先我认为这是groovy模板中的一个缺陷,但现在看来这是一个众所周知的限制,每个人都建议需要修改模板的解决方案:
(我尝试使用FreeMarker模板,但面临相同的问题)看看
groovy.util.ConfigSlurper#parse(java.util.Properties)
。用法:
template.make(new ConfigSlurper().parse(props))
template.make()
需要一个包含模板属性的对象,例如:
[a: [b: 'http://my.com/test']]
ConfigSlurper
执行将属性
实例转换为此类对象所需的操作