0

I'm developing a simple mail sender as Java EE application.

The project structure is shown as follows:

Java EE app proj structure

To properly setup email contents, I need to read the *.vm files placed inside the resource folder, that I supposed to have as path classpath:/templates/mail/*.vm (as with Spring)... But my supposition is wrong!

  1. Which is the right path to use?

  2. Should I have to use the META-INF folder? Is this solution more java-ee-compliant? In that case, where have I to put the META-INF folder inside my project structure?


Update:

I packaged the project as .war, then I putted the files in:

/src/main/webapp/WEB-INF/classes/templates/mail/

Then:

org.apache.velocity.Template t = myVelocityEngine.getTemplate("classpath:/templates/mail/account_to_confirm.vm",
                    "UTF-8");

Nonetheless, the app returns an error at runtime:

Unable to find resource 'classpath:/templates/mail/account_to_confirm.vm'

What am I doing wrong?


Just to better understand:

Supposing that I'd like to deploy this app as jar (removing the servlet class, of course): in that case, should I have to edit the folder layout in order to still use the same path into the source code?

vdenotaris
  • 13,297
  • 26
  • 81
  • 132

2 Answers2

1

If you can, use Classloader.getResourceAsStream("templates/mail/*.vm"); or similar getResourceAsURL method.

If not, take a look at where files from resources are placed inside WAR. In your case, the file should be in /WEB-INF/classes/templates/mail .

jderda
  • 890
  • 6
  • 18
  • I'd like to deploy this project as jar. I defined a servlet just to test the code. – vdenotaris May 23 '14 at 08:49
  • in such case, the files would be located in / of JAR, and classpath would be /META-INF/classes (as far as I remember, please use ZIP to verify it). You can put META-INF directory in your resources, it's content would be merged with META-INF generated during compilation – jderda May 23 '14 at 08:53
  • Should I to put the `META-INF` folder in `/src/main/resources`? In that case, which path have I to use in my code to retrieve the files? – vdenotaris May 23 '14 at 08:59
  • It depends on how you intend to use the template. Please, post the code fragment you are using and I'll try to answer. But the classpath:path you suggested looks correct for given setup (but only if you package it as a JAR file - if you package it as a WAR file, the directories layout is different, maybe that's the problem here? have you tested it as a JAR, not a WAR?) – jderda May 23 '14 at 09:07
  • Now, I'm packaging the project as war in order to properly run it on my Application Server, thus probably I'm using a bad directory layout. In other words, in this moment I have to use webapp folder layout, then archive folder layout. – vdenotaris May 23 '14 at 09:10
  • 1
    For war testing it'd be fastest to copy the resources into /WEB-INF/classes directory using for example http://maven.apache.org/plugins/maven-resources-plugin/examples/copy-resources.html. You can also change the directory layout and put everything you have in main/resources/ into main/resources/WEB-INF/classes/ directory. This should help, but remember to revert the changes when going back to JAR packaging – jderda May 23 '14 at 09:24
  • I've put my files in src/main/webapp/WEB-INF/classes/templates/mail, but at runtime, the app returns this error: "Unable to find resource 'templates/mail/account_to_confirm.vm'" – vdenotaris May 23 '14 at 09:44
1

I think the problem is due to the prefix classpath:: where did you find that you have to use it?

You might find useful understanding how to initialize VelocityEngine reading Loading velocity template inside a jar file and how Configuring Resource Loaders in Velocity.

Community
  • 1
  • 1
taringamberini
  • 2,719
  • 21
  • 29