我正在使用以下代码从模板中创建设置文件:
def config_template = this.class.getResource('/config.txt.template')
def template = new groovy.text.StreamingTemplateEngine(config_template)
def template_data = [
data1: [
datadict1: this.class.getResource('file1.xml'),
datadict2: this.class.getResource('file2.xml'),
cert: this.class.getResource('cert1.jks'),
],
data2: [
datadict1: this.class.getResource('file3.xml'),
datadict2: this.class.getResource('file4.xml'),
cert: this.class.getResource('cert2.jks'),
]
String sessions_config_text = template.make(template_data)
File sessions_config = new File().with { f ->
f.withWriter('UTF-8') { w ->
w.write(sessions_config_text)
}
return f
}
该代码被编译并打包到jar文件中。当我运行它时,我得到:
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:53)
at java.lang.Thread.run(Thread.java:745)
Caused by: groovy.lang.MissingMethodException: No signature of method: groovy.text.StreamingTemplateEngine.make() is applicable for argument types: (java.util.LinkedHashMap) values: [[data1:[datadict1:null, datadict2:null, cert:null, ...]]
Possible solutions: wait(), any(), wait(long), use([Ljava.lang.Object;), each(groovy.lang.Closure), any(groovy.lang.Closure)
这让我觉得StreamingTemplateEngine.make()
只会采用groovy的Map
类型,但当代码编译成java时,Map
会变成LinkedHashMap
和{{1方法签名变得无效。
这是对的吗?
如果是这样,我怎样才能让模板引擎工作?
如果没有,我做错了什么?如何解决?