Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 什么';这是放置freemarker模板文件的最佳实践_Java_Maven_Freemarker - Fatal编程技术网

Java 什么';这是放置freemarker模板文件的最佳实践

Java 什么';这是放置freemarker模板文件的最佳实践,java,maven,freemarker,Java,Maven,Freemarker,请参考我目前的做法。 它在一段时间内运行良好,我认为所有问题都已解决。但是,当我在不同的文件夹中构建jar时,抛出了“Template index.ftl not found”。我使用jarxf xxx.jar提取目标jar,发现templates文件夹下的*.ftl已经压缩到该jar中 我试图将以下配置添加到pom.xml中,但没有成功 <plugin> <!-- Build an executable JAR --> <groupId>org.ap

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

我试图将以下配置添加到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>

org.apache.maven.plugins
maven jar插件
3.1.0
符合事实的
自由基/
com.gearon.app.app
**/*.类
**/*杰多先生
**/*.物业
**/*.xml
**/*.ftl
OP还说:

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

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

但是加载模板的方式应该改变,对吗?目前是

setClassForTemplateLoading(Config.class,“/templates”);


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

Maven committer here


你所做的完全是错误的,你违反了惯例。它表示运行时类路径中必须可用的所有资源必须位于
src/main/resources
中。无需进一步配置。我强烈建议这样做。在您的例子中:
src/main/resources/templates
,您就完成了。没有必要在JAR插件中添加

最好不要在文章标题中使用“最佳实践”一词。不清楚你想做什么。但是,如果只是将模板放在
src/main/resources/templates/
中,这是一种非常常见的方法,也不需要任何Maven默认配置。但是,
Config.class
是否与模板在同一个jar中?@ddekany是的,Config.class与模板处于同一级别。我试着把templates/***.ftl放在src/main/resources下,它又能工作了。感谢您对更改内容的评论?顺便说一句,之所以需要
***.ftl
,是因为您已经指定了
包含
。否则,所有内容都包含在
资源中
。我以前没有将模板放在src/main/resources中,而是放在src/main/java中,我认为模板没有添加到jar文件中的原因是。