Find Questions…

Close ×
First time here? Check out the FAQ!

Use velocity template in Jira plugin

Andreas Lundström asked this question · 11 karma ·


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 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.


One Answer:

Andy Brook [The Plugin People] · 15,368 karma ·

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 · 11 karma ·

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:

Methods inherited from class com.atlassian.velocity.DefaultVelocityManager
getBody, getBody, getEncodedBody, getEncodedBody, getEncodedBody, getEncodedBodyForContent, getErrorMessageForException

Andy Brook [The Plugin People] · 15,368 karma ·

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.

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 · 11 karma ·

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 [The Plugin People] · 15,368 karma ·

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 · 11 karma ·

Thanks a lot for helping out Andy (: !

Looking for something else?

Find Questions…

or Browse other questions tagged:

or Ask a Question