我应该将使用 Maven 构建的命令行实用程序的 Velocity 模板文件放在哪里?

发布于 2024-07-12 04:01:44 字数 1249 浏览 7 评论 0原文

我有一个小型命令行实用程序项目,我使用 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 并让测试通过的唯一位置是在根目录中项目,即作为 srctarget 的对等体。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

温折酒 2024-07-19 04:01:44

您可以通过编程方式配置 TEMPLATE_ROOT,如下所示:

Properties props = new Properties();        
props.put("file.resource.loader.path", templateRootDir);

VelocityEngine engine = new VelocityEngine();
engine.init(props);
engine.evaluate(...);

You can programmatically configure TEMPLATE_ROOT as follows:

Properties props = new Properties();        
props.put("file.resource.loader.path", templateRootDir);

VelocityEngine engine = new VelocityEngine();
engine.init(props);
engine.evaluate(...);
于我来说 2024-07-19 04:01:44

您应该将它们放在 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.

鹿港小镇 2024-07-19 04:01:44

所以事实证明,

generator.mergeTemplate("foo.yaml", writer);

我不应该使用类似的东西,而应该使用类似的东西

InputStream fooStream = getClass().getResourceAsStream("foo.yaml");
generator.mergeTemplate(fooStream, writer);

So it turns out that instead of using something like

generator.mergeTemplate("foo.yaml", writer);

I should use something like

InputStream fooStream = getClass().getResourceAsStream("foo.yaml");
generator.mergeTemplate(fooStream, writer);
李白 2024-07-19 04:01:44

您只需将 Velocity 配置为使用 ClasspathResourceLoader,而不是默认的 FileResourceLoader。

You could just configure Velocity to use the ClasspathResourceLoader, instead of the default FileResourceLoader.

夏雨凉 2024-07-19 04:01:44

我尝试过 Velocity.setProperty() 设置类似于 @Jin Kim 上面所说的属性,并且能够运行它。

VelocityEngine ve = new VelocityEngine();
ve.setProperty(RunTimeConstants.RESOURCE_LOADER,"file");
ve.setProperty("file.resource.loader.path",templaterootdir);

ve.init();

I have tried Velocity.setProperty() to set the properties similar to what was said above by @Jin Kim and was able to run it.

VelocityEngine ve = new VelocityEngine();
ve.setProperty(RunTimeConstants.RESOURCE_LOADER,"file");
ve.setProperty("file.resource.loader.path",templaterootdir);

ve.init();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文