Usage of velocity for plugin development

Answered

Idea is to create a predefined class based on configuration through UI.
Created an Action which pops up UI to get the configuration.
The predefined custom code is stored as a velocity template inside plugin resources.
While using below code to load template it throws error (Resource Not found)
VelocityEngine velocityEngine = new VelocityEngine();
velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
velocityEngine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
velocityEngine.setProperty("runtime.log.logsystem.class","org.apache.velocity.runtime.log.NullLogSystem");
velocityEngine.init();
Template t = velocityEngine.getTemplate("EntityModel.vm");

2
5 comments

I'm sorry, I don't understand your question.

If you get the Resource Not found error message, you probably should revise your path.

0

@Jakub Chrzanowski

I'm trying to load a template that is in the plugin resources.

Placed the velocity template in the resources. While executing the plugin the velocity engine not able to load the resources.

(Note) Executing the code with the main function it works but not when it is ex ecuted as the plugin. 

Guessing that there might be an issue with ( ClassLoader / ResourceLoader ) of velocity code while using it with the IntelliJ plugin code. 

Thanks for the response.

1

I'm having a similar issue - the velocity classpathresourceloader does not find the resources directory on the classpath. I can work around this by loading the template as a string directly, eg:

try (Reader reader = new InputStreamReader(
Objects.requireNonNull(this.getClass().getClassLoader().getResourceAsStream(getTemplateName()) ) )) {
ve.evaluate(vc, sw, "Generator", reader);
}

But I cannot use, eg, the #parse directive if I do this

 

 

1

Lordfoom

Used below function it worked for me. hope it helps you. :)

        VelocityEngine velocityEngine = new VelocityEngine();
        velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
        velocityEngine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
        velocityEngine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, new NullLogChute());
        velocityEngine.init();
        VelocityContext context = new VelocityContext();
        InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("VelocityTemplate.vm");
        InputStreamReader inputStreamReader = new InputStreamReader(resourceAsStream);
        velocityEngine.evaluate(context, writer, "ERROR", inputStreamReader);
1

Thanks Vigneshj6 - that is helpful. The only problem is it means you can't use #parse or #include directives in the template files. Very frustating.

0

Please sign in to leave a comment.