Use velocity template in Jira plugin

Andreas Lundström June 10, 2012

Hi,

How can I init() velocity and make use of a velocity template within JIRA for something other than, for example, a customfield?

Say that I wanted to make a custom message as a comment or something - upon issue created via an event listener, lets say I want something simple like using the example from http://velocity.apache.org/engine/devel/developer-guide.html#thevelocityhelperclass but having the variables "name" and "project" set via selections in custom fields?

EDIT: if I used the example from apache I get this error..

The specified class for ResourceManager (org.apache.velocity.runtime.resource.ResourceManagerImpl) does not implement org.a
pache.velocity.runtime.resource.ResourceManager; Velocity is not initialized correctly.

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

2 votes
Answer accepted
Andy Brook [Plugin People]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 10, 2012

There seem to be at least two questions in there, here is an answer regarding Velocity initialisation:

There are a bunch of ways that you can initialize Velocity. Depending on your context (heh). Simply you could do this:

Map<String,Object> context = new HashMap<String,Object>();
// add your context stuff here

VelocityManager velocityManager = ComponentManager.getInstance().getVelocityManager();
ApplicationProperties applicationProperties=ComponentManager.getInstance().getApplicationProperties();
String baseUrl=applicationProperties.getString(APKeys.JIRA_BASEURL);
String webworkEncoding = applicationProperties.getString(APKeys.JIRA_WEBWORK_ENCODING);

String renderedText = velocityManager.getEncodedBody(DIRNAME_PATH, FILENAME, baseUrl, webworkEncoding, context);

You can add whatever inputs you need, and use velocity markup to manipulate those values and render.

The injected helpers are for when you have an Xwork action /other, and want those helpers to be available in the velocity context for rendered templates. Im not exactly clear on what you're trying to do.

Andreas Lundström June 10, 2012

Hi, and thanks for your reply! I was wondering about this:

String renderedText = velocityManager.getEncodedBody(DIRNAME_PATH, FILENAME, baseUrl, webworkEncoding, context);

is DIRNAME_PATH and FILENAME supposed to be:

String dirname = "src/main/";
String filename = "mytemplate.vm";

I tried that but my renderedText is set to null, what am I doing wrong? If you could give me a pointer or two (:

I looked for more on Jira/Atlassian VelocityManager, but didnt find more on the methods:

<th align="left">
getBody, getBody, getEncodedBody, getEncodedBody, getEncodedBody, getEncodedBodyForContent, getErrorMessageForException

Andy Brook [Plugin People]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 10, 2012

DIRNAME_PATH refers to an actual template .vm file thats packaged with your plugin, it would be relative, eg under src/main/resources (source path), you would have 'templates/yourplugin/thing.vm', so DIRNAME_PATH = "templates/yourplugin/"; // note the trailing / ... the number of times...

FILENAME is then just "thing.vm". k?

Regarding impl, you dont say what JIRA rev you are using. Im at 5.0.x? Also, don't use the impls, use the interfaces, use dependency injection or the ComponentManager/ComponentAccessor to retrieve implementations.

http://docs.atlassian.com/atlassian-velocity/0.1.9/com/atlassian/velocity/VelocityManager.html

getEncodedBodyForContent is interesting as it doesnt use resources packed with your plugin, it can use derived/retrieved data to the the rendering. This is good and bad, in that templates wouldn't therefore get cached, and will put more io load on the app to retrieve said template....

Andreas Lundström June 10, 2012

Yeah, thats what I thought before I posted the reply above, but I get an error that the template couldnt be found although it is present at "<my plugin>/src/main/resources/mytemplate.vm":

Could not find template 'src/main/resources/mytemplate.vm' ensure it is in the classpath.

What I did was:

...
String renderedText = velocityManager.getEncodedBody("src/main/resources/", "mytemplate.vm", baseUrl, webworkEncoding, context);
...

Or does the template have to reside within the actual Jira install-dir and not within the plugin?

A follow-up question, can the .vm be added to the atlassian-plugin.xml as some other kind of resource (other than view/edit/create/search, for like customfields and such..)? I read a post on the old jira forums for something in that fashion but I cannot seem to find it now ):

Andy Brook [Plugin People]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 10, 2012

I think there is some confusion regarding compile time and deployment time paths. src/main/resources is only applicable at DEV time. Once compiled, you will find all resources and paths under /src/main/resources become 'top level' as far as your output JAR is concerned, for example, a folder located in your /src/main/resources/templates directory will become /templates after compilation and inclusion in a jar.

Regarding your second question (2 for the price of one?!) you _only_ need to specify a .vm resource in the plugin.xml file if it is required, such as for an XWork action. If you are doing a 'roll your own' velocity render, then you do not need to specify it anywhere.

Andreas Lundström June 10, 2012

Thanks a lot for helping out Andy (: !

TAGS
AUG Leaders

Atlassian Community Events