How can I expose custom variables to Velocity templates in JIRA plugin development?

Hey everyone,

I’m working on a JIRA plugin and I’m stuck. I’m trying to make some custom variables available in my Velocity templates, but I can’t figure out how to do it.

Here’s what I’ve got so far:

public class CustomAction extends AbstractIssueSelectAction {
    private final Issue customIssue;

    public String executeAction() throws Exception {
        log.debug("Executing custom action");
        return "custom-template";
    }
}

In my template, I can use ${issue.summary} just fine, but I want to use something like ${customIssue.summary} for a different issue.

I’ve looked through the code with a debugger, but it’s pretty complicated and I couldn’t figure it out. The Webwork stuff is especially tricky to understand.

Has anyone done this before? Any tips on how to add my own variables to the Velocity context? I’d really appreciate any help!

Thanks a bunch!

I’ve encountered this issue before while developing JIRA plugins. One effective approach is to override the getVelocityParameters() method in your action class. This method is called by the framework to populate the Velocity context.

Here’s how you can implement it:

@Override
protected void getVelocityParameters(Map<String, Object> params) {
super.getVelocityParameters(params);
params.put(“customIssue”, customIssue);
}

This method allows you to add your custom variables to the params map, which is then used to populate the Velocity context. Make sure your customIssue field is properly initialized before this method is called.

If you’re still facing issues, consider using the VelocityParam annotation if you’re working with a recent version of JIRA or Atlassian SDK. It can simplify the process of adding variables to the context.

Remember to consult the official Atlassian documentation for the most up-to-date information on JIRA plugin development best practices.

I’ve been down this road before, and I feel your pain. Getting custom variables into Velocity templates can be a bit tricky. Here’s what worked for me:

In your action class, try overriding the getVelocityParameters() method. This method is called by the framework to populate the Velocity context. You can add your custom variables here:

@Override
protected void getVelocityParameters(Map<String, Object> params) {
    super.getVelocityParameters(params);
    params.put("customIssue", customIssue);
}

This way, you’re working with the framework instead of trying to manipulate the context directly. It’s much cleaner and less likely to cause issues down the line.

Also, make sure your customIssue field is properly initialized before this method is called. If you’re having trouble, try setting up some logging to verify that your custom variable is being added correctly.

Hope this helps! Let me know if you run into any other roadblocks.

hey man, i feel ur pain. ive dealt with this before. try overriding getVelocityParameters() in ur action class. it’s called by the framework to fill the velocity context. add ur custom vars there like this:

@Override
protected void getVelocityParameters(Map<String, Object> params) {
super.getVelocityParameters(params);
params.put(“customIssue”, customIssue);
}

hope that helps! lmk if u need anything else