Search code examples
javamavenfreemarker

What's the best practice to put freemarker template files


Please refer to this thread about my current practice. It worked well for a period of time and I thought all issues had been figured out. However, when I built the jar in different folder, "Template index.ftl not found" was thrown. I use jar xf xxx.jar to extract target jar and found *.ftl under templates folder has been compressed into that jar.

I tried solution here to add below configuration to pom.xml but it didn't work.

<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>

The OP also said:

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

I'd like to give it a try to put the templates/xxx.ftl to src/main/resources but not src/main/java/com/gearon/app/templates/*.ftl.

But the way to load template should be changed right? Currently, it's cfg.setClassForTemplateLoading(Config.class, "/templates");

So here comes the question, how to change it? Or if my understanding above is totally wrong, what's the best practice to put templates into a jar and make sure the class into that jar can find those templates?


Solution

  • Maven committer here...

    What you are doing is plain wrong, you are going against the conventions. It says that all resources which have to be available in the runtime classpath must reside in src/main/resouces. There is no further configuration necessary. I'd highly advise to do that. In your case: src/main/resources/templates and you are done. No <includes /> in the JAR Plugin necessary.