0

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?

Eugene
  • 10,627
  • 5
  • 49
  • 67
  • It would be better to not have the phrase “best practice” in your post title. – 0xCursor Aug 05 '18 at 04:48
  • It's not clear what do you try to do. But if it's just putting the templates inside `src/main/resources/templates/`, that's a very common way of doing it, and doesn't require any Maven configuration from its defaults either. However, is `Config.class` inside the same jar as the templates? – ddekany Aug 05 '18 at 06:03
  • @ddekany Yes, the Config.class is at the same level of templates. I tried to put templates/***.ftl under src/main/resources and it works again. Thanks for your comment. – Eugene Aug 05 '18 at 06:17
  • @Gearon What was changed? BTW, the `**/*.ftl` is only needed because you have specified the `includes`. Otherwise everything is included from `resources`. – ddekany Aug 05 '18 at 06:21
  • I haven't put templates to src/main/resources but put it to src/main/java before and I think that the reason why templates hasn't been added to the jar file. – Eugene Aug 05 '18 at 06:26

1 Answers1

1

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.

Michael-O
  • 18,123
  • 6
  • 55
  • 121