我应该将使用 Maven 构建的命令行实用程序的 Velocity 模板文件放在哪里?
我有一个小型命令行实用程序项目,我使用 Maven 来管理它。 该实用程序是一个非常简单的应用程序,用于填充 Velocity 模板并将结果转储到新文件中。 我的问题是在哪里放置 Velocity 模板。 当我将它们放入 src/test/resources/foo/bar/baz
中时,mvn test
失败,因为它找不到引用的模板,即使它显然在那里在 target/classes/foo/bar/baz
中,这是测试的 .class
文件和被测类所在的位置。 如果我将模板放在项目的顶级目录中,测试会通过,但是我没有遵循 Maven 项目结构,并且我怀疑实际打包的 .jar 文件无法运行。 我缺少什么?
更新:
测试方法:
public final void mergeTemplate(final String templateFileName, final Writer writer) throws ResourceNotFoundException, ParseErrorException, MethodInvocationException, IOException, Exception {
Velocity.init();
Velocity.mergeTemplate(templateFileName, Charset.defaultCharset().name(), context(), writer);
}
测试方法:
@Test
public void testMergeTemplate() throws Exception {
final FooGenerator generator = new FooGenerator();
final StringWriter writer = new StringWriter();
generator.mergeTemplate("foo.yaml", writer);
Assert.assertEquals("Something went horribly, horribly wrong.", EXPECTED_RESULT, writer.toString().trim());
}
我可以放置 foo.yaml
并让测试通过的唯一位置是在根目录中项目,即作为 src
和 target
的对等体。
I have a small command line utility project that I'm using Maven to manage. The utility is a very simple app to populate a Velocity template and dump the results to a new file. My problem is where to put my Velocity templates. When I put them in src/test/resources/foo/bar/baz
, mvn test
fails because it can't find the referenced template, even though it is clearly there in target/classes/foo/bar/baz
, which is where the .class
file for the test and the class under test are located. If I put the template in the top-level directory of the project, the test passes, but then I'm not following the Maven project structure, and I suspect that the actual packaged .jar file wouldn't function. What am I missing?
UPDATE:
Method under test:
public final void mergeTemplate(final String templateFileName, final Writer writer) throws ResourceNotFoundException, ParseErrorException, MethodInvocationException, IOException, Exception {
Velocity.init();
Velocity.mergeTemplate(templateFileName, Charset.defaultCharset().name(), context(), writer);
}
Test method:
@Test
public void testMergeTemplate() throws Exception {
final FooGenerator generator = new FooGenerator();
final StringWriter writer = new StringWriter();
generator.mergeTemplate("foo.yaml", writer);
Assert.assertEquals("Something went horribly, horribly wrong.", EXPECTED_RESULT, writer.toString().trim());
}
The only place I can place foo.yaml
and have the tests pass is in the root directory of the project, i.e., as a peer of src
and target
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以通过编程方式配置 TEMPLATE_ROOT,如下所示:
You can programmatically configure TEMPLATE_ROOT as follows:
您应该将它们放在 src/main/resources/foo/bar/baz 中,因为它们需要包含在主 jar 文件中。
You should put them in src/main/resources/foo/bar/baz because they need to be included in the main jar file.
所以事实证明,
我不应该使用类似的东西,而应该使用类似的东西
So it turns out that instead of using something like
I should use something like
您只需将 Velocity 配置为使用 ClasspathResourceLoader,而不是默认的 FileResourceLoader。
You could just configure Velocity to use the ClasspathResourceLoader, instead of the default FileResourceLoader.
我尝试过 Velocity.setProperty() 设置类似于 @Jin Kim 上面所说的属性,并且能够运行它。
I have tried Velocity.setProperty() to set the properties similar to what was said above by @Jin Kim and was able to run it.