代码之家  ›  专栏  ›  技术社区  ›  Eugene

放置freemarker模板文件的最佳实践是什么

  •  0
  • Eugene  · 技术社区  · 7 年前

    请参考 this thread 关于我目前的做法。 有一段时间效果不错,我觉得所有的问题都解决了。但是,当我在不同的文件夹中构建jar时,抛出了“Template index.ftl not found”。我用 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>
    

    评论还说:

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

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

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

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

    1 回复  |  直到 7 年前
        1
  •  1
  •   Michael-O    7 年前

    这里是Maven committer。。。

    你所做的完全是错误的,你违背了惯例。它表示运行时类路径中必须可用的所有资源必须位于 src/main/resouces . 无需进一步配置。我强烈建议你那样做。就你而言: src/main/resources/templates 你就完了。不 <includes /> 必要的JAR插件。

    推荐文章