放置 freemarker 模板文件的最佳做法是什么
What's the best practice to put freemarker template files
请参考我目前的做法。
它在一段时间内运作良好,我认为所有问题都已解决。但是,当我在不同的文件夹中构建 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>
OP 还说:
Better yet, I removed the configuration tag entirely, and it's still
working. I think it was a remnant from before I figured out that the
.properties files and other things I needed on the classpath needed to
be in src/main/resources and not src/main/java
我想尝试将 templates/xxx.ftl 改为 src/main/resources 而不是 src/main/java/com/gearon/app/templates/*.ftl.
但是加载模板的方式应该改变了吧?目前,它是
cfg.setClassForTemplateLoading(Config.class, "/templates");
那么问题来了,怎么改呢?或者,如果我上面的理解完全错误,那么将模板放入 jar 并确保放入该 jar 的 class 可以找到这些模板的最佳做法是什么?
这里是 Maven 提交者...
你这样做是完全错误的,你违背了惯例。它表示必须在运行时类路径中可用的所有资源都必须驻留在 src/main/resouces
中。无需进一步配置。我强烈建议这样做。在您的情况下:src/main/resources/templates
就完成了。 JAR 插件中不需要 <includes />
。
请参考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 还说:
Better yet, I removed the configuration tag entirely, and it's still working. I think it was a remnant from before I figured out that the .properties files and other things I needed on the classpath needed to be in src/main/resources and not src/main/java
我想尝试将 templates/xxx.ftl 改为 src/main/resources 而不是 src/main/java/com/gearon/app/templates/*.ftl.
但是加载模板的方式应该改变了吧?目前,它是
cfg.setClassForTemplateLoading(Config.class, "/templates");
那么问题来了,怎么改呢?或者,如果我上面的理解完全错误,那么将模板放入 jar 并确保放入该 jar 的 class 可以找到这些模板的最佳做法是什么?
这里是 Maven 提交者...
你这样做是完全错误的,你违背了惯例。它表示必须在运行时类路径中可用的所有资源都必须驻留在 src/main/resouces
中。无需进一步配置。我强烈建议这样做。在您的情况下:src/main/resources/templates
就完成了。 JAR 插件中不需要 <includes />
。