【问题标题】:What's the best practice to put freemarker template files放置freemarker模板文件的最佳做法是什么
【发布时间】:2019-01-12 11:01:04
【问题描述】:

请参考this thread我目前的做法。 它运行了一段时间,我认为所有问题都已解决。但是,当我在不同的文件夹中构建 jar 时,抛出了“找不到模板 index.ftl”。我使用jar xf xxx.jar提取目标jar,发现templates文件夹下的*.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 中的类可以找到这些模板的最佳做法是什么?

【问题讨论】:

  • 最好不要在帖子标题中使用“最佳实践”一词。
  • 不清楚你想做什么。但是如果只是将模板放在src/main/resources/templates/ 中,这是一种非常常见的做法,并且也不需要任何 Maven 的默认配置。但是,Config.class 是否与模板在同一个 jar 中?
  • @ddekany 是的,Config.class 与模板处于同一级别。我尝试将 templates/***.ftl 放在 src/main/resources 下,它又可以工作了。感谢您的评论。
  • @Gearon 发生了什么变化?顺便说一句,仅需要 &lt;include&gt;**/*.ftl&lt;/include&gt;,因为您已指定 includes。否则,所有内容都包含在 resources 中。
  • 我之前没有把模板放到src/main/resources但是把它放到src/main/java,我认为是jar文件没有添加模板的原因。跨度>

标签: java maven freemarker


【解决方案1】:

这里是 Maven 提交者...

你所做的是完全错误的,你违反了惯例。它表示所有必须在运行时类路径中可用的资源必须驻留在src/main/resouces 中。无需进一步配置。我强烈建议这样做。在您的情况下:src/main/resources/templates,您就完成了。 JAR 插件中不需要&lt;includes /&gt;

【讨论】:

    猜你喜欢
    • 2011-11-27
    • 1970-01-01
    • 2010-09-29
    • 2010-12-16
    • 2013-01-08
    • 2016-05-19
    • 2013-09-20
    • 2015-04-30
    • 2017-04-21
    相关资源
    最近更新 更多