在 jar 文件中加载速度模板
我有一个项目,我想加载速度模板以使用参数完成它。整个应用程序被打包为一个 jar 文件。我最初想做的是:
VelocityEngine ve = new VelocityEngine();
URL url = this.getClass().getResource("/templates/");
File file = new File(url.getFile());
ve = new VelocityEngine();
ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "file");
ve.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, file.getAbsolutePath());
ve.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_CACHE, "true");
ve.init();
VelocityContext context = new VelocityContext();
if (properties != null) {
stringfyNulls(properties);
for (Map.Entry<String, Object> property : properties.entrySet()) {
context.put(property.getKey(), property.getValue());
}
}
final String templatePath = templateName + ".vm";
Template template = ve.getTemplate(templatePath, "UTF-8");
String outFileName = File.createTempFile("report", ".html").getAbsolutePath();
BufferedWriter writer = new BufferedWriter(new FileWriter(new File(outFileName)));
template.merge(context, writer);
writer.flush();
writer.close();
当我在 Eclipse 中运行它时,它工作得很好。但是,一旦我打包程序并尝试使用命令行运行它,就会收到错误,因为找不到文件。
我想问题出在这一行:
ve.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, file.getAbsolutePath());
因为在 jar 中,绝对文件不存在,因为它位于 zip 中,但我还找不到更好的方法来做到这一点。
有人有什么想法吗?
I have a project where I want to load a velocity template to complete it with parameters. The whole application is packaged as a jar file. What I initially thought of doing was this:
VelocityEngine ve = new VelocityEngine();
URL url = this.getClass().getResource("/templates/");
File file = new File(url.getFile());
ve = new VelocityEngine();
ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "file");
ve.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, file.getAbsolutePath());
ve.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_CACHE, "true");
ve.init();
VelocityContext context = new VelocityContext();
if (properties != null) {
stringfyNulls(properties);
for (Map.Entry<String, Object> property : properties.entrySet()) {
context.put(property.getKey(), property.getValue());
}
}
final String templatePath = templateName + ".vm";
Template template = ve.getTemplate(templatePath, "UTF-8");
String outFileName = File.createTempFile("report", ".html").getAbsolutePath();
BufferedWriter writer = new BufferedWriter(new FileWriter(new File(outFileName)));
template.merge(context, writer);
writer.flush();
writer.close();
And this works fine when I run it in eclipse. However, once I package the program and try to run it using the command line I get an error because the file could not be found.
I imagine the problem is in this line:
ve.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, file.getAbsolutePath());
Because in a jar the absolute file does not exist, since it's inside a zip, but I couldn't yet find a better way to do it.
Anyone has any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果你想使用类路径中的资源,你应该使用类路径的资源加载器:
If you want to use resources from classpath, you should use resource loader for classpath:
最终代码,使用上面两个答案中提出的想法开发:
Final code, developed using the ideas presented in both answers above:
除非JAR被分解,否则您无法将JAR中的资源作为文件读取。使用输入流。
请参阅以下代码片段,
Unless JAR is exploded, you can't read the resource in JAR as file. Use an input stream.
See following code snippets,
要使 Velocity 在类路径中查找模板:
To make Velocity look for the templates in classpath:
也许我有一个旧版本,这是唯一对我有用的东西
Maybe I have an old version, this is the only thing that worked for me