放置自由标记模板文件的最佳做法是什么

时间:2018-08-05 04:46:59

标签: java maven freemarker

请参考this thread,了解我目前的做法。 它在一段时间内运行良好,我认为所有问题都已解决。但是,当我在其他文件夹中构建jar时,抛出了“找不到模板index.ftl”。我使用jar xf xxx.jar提取目标jar,发现template文件夹下的* .ftl已压缩到该jar中。

我尝试solution here将以下配置添加到pom.xml中,但是没有用。

<plugin>
  <!-- Build an executable JAR -->
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  <version>3.1.0</version>
  <configuration>
    <archive>
      <manifest>
        <addClasspath>true</addClasspath>
        <classpathPrefix>libs/</classpathPrefix>
        <mainClass>com.gearon.app.App</mainClass>
      </manifest>
    </archive>
    <includes>
        <include>**/*.class</include>
        <include>**/*.jdo</include>
        <include>**/*.properties</include>
        <include>**/*.xml</include>
        <include>**/*.ftl</include>
    </includes>
  </configuration>
</plugin>

OP还说:

  

更好的是,我完全删除了配置标签,但它仍然   工作。我认为这是我发现   .properties文件和我需要的类路径上的其他东西   在src / main / resources中,而不是src / main / java

我想尝试将模板/xxx.ftl放到src / main / resources,而不是src / main / java / com / gearon / app / templates / *。ftl。

但是应该更改加载模板的方式对吗?目前是 cfg.setClassForTemplateLoading(Config.class, "/templates");

因此,问题来了,如何更改?或者,如果我的理解是完全错误的,将模板放入jar中并确保该jar中的类可以找到这些模板的最佳实践是什么?

1 个答案:

答案 0 :(得分:1)

这里是Maven提交者...

您正在做的事情是完全错误的,您违反了约定。它说所有必须在运行时类路径中可用的资源都必须驻留在src/main/resouces中。无需其他配置。我强烈建议您这样做。对于您的情况:src/main/resources/templates,您就完成了。在JAR插件中无需<includes />