Just a simple velocity standalone app based on maven structure. Here is the code snippet written in Scala to render the template helloworld.vm
in ${basedi
You can just use it like this:
Template t = ve.getTemplate("./src/main/resources/templates/email_html_new.vm");
It works.
You can also put your templates folder under src/main/resources instead of src/main/java. Works for me and it's a preferable location since templates are not java source.
Great question - I solved my issue today as follows using Ecilpse:
Put your template in the same folder hierarchy as your source code (not in a separate folder hierarchy even if you include it in the build path) as below:
In your code simply use the following lines of code (assuming you just want the date to be passed as data):
VelocityEngine ve = new VelocityEngine();
ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
ve.init();
VelocityContext context = new VelocityContext();
context.put("date", getMyTimestampFunction());
Template t = ve.getTemplate( "templates/email_html_new.vm" );
StringWriter writer = new StringWriter();
t.merge( context, writer );
See how first we tell VelocityEngine to look in the classpath. Without this it wouldn't know where to look.
VelocityEngine velocityEngin = new VelocityEngine();
velocityEngin.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
velocityEngin.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
velocityEngin.init();
Template template = velocityEngin.getTemplate("nameOfTheTemplateFile.vtl");
you could use the above code to set the properties for velocity template. You can then give the name of the tempalte file when initializing the Template and it will find if it exists in the classpath.
All the above classes come from package org.apache.velocity*
I faced the similar problem with intellij IDEA. you can use this
VelocityEngine ve = new VelocityEngine();
Properties props = new Properties();
props.put("file.resource.loader.path", "/Users/Projects/Comparator/src/main/resources/");
ve.init(props);
Template t = ve.getTemplate("helloworld.vm");
VelocityContext context = new VelocityContext();
Make sure you have a properly configured resource loader. See Velocity documentation for help selecting and configuring a resource loader: http://velocity.apache.org/engine/releases/velocity-1.7/developer-guide.html#resourceloaders