Can Velocity templates be used in Bitbucket Server Plugins?

Hi,

I’ve seen some example usages of Velocity templates and wanted to use it for my plugin. I found this link: “https://developer.atlassian.com/server/confluence/” and thought it could be very useful in the plugin that I want to build for Bitbucket Server. However, I could not find anything on Velocity templates in Bitbucket Server plugins. So I would like to know if it is possible to use Velocity templates for Bitbucket Server plugins.

Thanks.

Hello!

I was able to use Velocity templates with my custom servlet. There are 4 steps to achieve that.

  1. Add dependencies to your pom.xml:
<dependency>
    <groupId>com.atlassian.templaterenderer</groupId>
    <artifactId>atlassian-template-renderer-api</artifactId>
    <version>${atr.version}</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>com.atlassian.bitbucket.server</groupId>
    <artifactId>bitbucket-api</artifactId>
    <version>${bitbucket.api.version}</version>
    <scope>provided</scope>
</dependency>

<bitbucket.api.version>7.2.2</bitbucket.api.version>
<atr.version>4.0.0</atr.version>
  1. Add section to atlassian-plugin.xml. For my case it looks like this:
<servlet name="BPMN Diff Page" 
               i18n-name-key="ru.domclick.bitbucket.bpmn-diff-servlet.name"
               key="ru.domclick.bpmn-diff-servlet"
               class="ru.domclick.bitbucket.BpmnDiffServlet">
      <url-pattern>/bpmn-diff</url-pattern>
</servlet>
  1. Add Velocity template file to your resources. In my case it was a file reources/velocity/bpmn-diff.vm.
  2. Implement servlet class.
package ru.domclick.bitbucket;

import com.atlassian.bitbucket.auth.AuthenticationContext;
import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;
import com.atlassian.templaterenderer.TemplateRenderer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Component
public class BpmnDiffServlet extends HttpServlet {
    private static final String TEMPLATE_PATH = "velocity/bpmn-diff.vm";

    private final TemplateRenderer templateRenderer;
    private final AuthenticationContext authenticationContext;

    @Autowired
    public BpmnDiffServlet(
            @ComponentImport TemplateRenderer templateRenderer,
            @ComponentImport AuthenticationContext authenticationContext
    ) {
        this.templateRenderer = templateRenderer;
        this.authenticationContext = authenticationContext;
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        if (authenticationContext.isAuthenticated()) {
            response.setContentType("text/html");
            Map<String, Object> params = new HashMap<>();
            templateRenderer.render(TEMPLATE_PATH, params, response.getWriter());
        }
    }
}

Hope this helps.