Tutorial: how to create an obr with dependent jar inside.

While developing a Jira plugin for Jira Server/Data Center we sometimes need to include a dependency inside our plugin because our Jira instance does not have the required dependency. To fulfill this requirement we could provide the Compile scope for the dependency and all classes from the dependency would be included in our jar file.

There is also another solution. We could add the dependency into the obr file of our plugin. And if we install our obr file into a Jira instance, our dependency will be also installed.

In this tutorial we will create two plugins. The first plugin is called jira-library. This plugin will have an exported service called LibraryService. This service will have only one method getLibraryMessage() which returns a text message "jira-library test message". The second plugin will contain a servlet which calls getLibraryMessage() from the first plugin and print out the text message to the user who invoked the servlet. We will build the sevlet plugin as an obr file with a jira-library jar file inside the plugin.

The source code of the two plugins is available here:

https://bitbucket.org/alex1mmm/jira-library

https://bitbucket.org/alex1mmm/jira-obr

Let's create jira-library plugin.

We need to execute the following command:

atlas-create-jira-plugin-module

We will  be asked to set several parameters for our plugin. I provide parameters with answers:

Define value for groupId: : ru.matveev.alexey.tutorial.library
Define value for artifactId: : jira-library
Define value for version:  1.0.0-SNAPSHOT: :
Define value for package:  ru.matveev.alexey.tutorial.library: :
 Y: : Y

After creating the plugin we need to add our LibraryService to the plugin. First we add the interface of our service (LibraryService.java file) to the ru.matveev.alexey.tutorial.library.api package

package ru.matveev.alexey.tutorial.library.api;

public interface LibraryService {
    String getLibraryMessage();
}

And then we add our service implementation to the ru.matveev.alexey.tutorial.library.impl package:

package ru.matveev.alexey.tutorial.library.impl;

import com.atlassian.plugin.spring.scanner.annotation.export.ExportAsService;
import ru.matveev.alexey.tutorial.library.api.LibraryService;

import javax.inject.Named;

@ExportAsService({LibraryService.class})
@Named
public class LibraryServiceImpl implements LibraryService {
    public String getLibraryMessage() {
        return "jira-library test message";
    }
}

 Then we create a jar file of the first plugin with

altas-mvn package

We can see the jar file in the target directory of the jira-library folder.

Let's create the second plugin.

Again we execute the following command:

atlas-create-jira-plugin

And provide the following answers:

Define value for groupId: : ru.matveev.alexey.tutorial.obr
Define value for artifactId: : jira-obr
Define value for version:  1.0.0-SNAPSHOT: :
Define value for package:  ru.matveev.alexey.tutorial.obr: :
 Y: : Y

Then we go to the jira-obr folder and create a servlet module inside jira-obr plugin with the following command:

atlas-create-jira-plugin-module

You need to provide the following answers:

Choose a number (1/2/3/4/5/6/7/8/9/10/11/12/13/14/15/16/17/18/19/20/21/22/23/24/25/26/27/28/29/30/31/32/33/34): 21
Enter New Classname MyServlet: :
Enter Package Name ru.matveev.alexey.tutorial.obr.servlet: :
Show Advanced Setup? (Y/y/N/n) N: : N
Add Another Plugin Module? (Y/y/N/n) N: : N

Then add the following dependency in the pom.xml file

<dependency>
            <groupId>ru.matveev.alexey.tutorial.library</groupId>
            <artifactId>jira-library</artifactId>
            <version>1.0.0-SNAPSHOT</version>
            <scope>system</scope>
            <systemPath>/home/alexm/atlasplugin/obr/jira-library/target/jira-library-1.0.0-SNAPSHOT.jar</systemPath>
</dependency>

Change /home/alexm/atlasplugin/obr/jira-library/target/jira-library-1.0.0-SNAPSHOT.jar to the real path to your jira-library-1.0.0-SNAPSHOT.jar.

I chose to save the jar file in the file system because in this case I do not need to install a repository manager to save my jar file. But in real life you should save your jar files in a repository manager like Artifactory or Nexus Repository or something similair.

 Then we add into configuration section of maven-jira-plugin of pom.xml

<pluginDependencies>
                        <pluginDependency>
                            <groupId>ru.matveev.alexey.tutorial.library</groupId>
                            <artifactId>jira-library</artifactId>
                        </pluginDependency>
 </pluginDependencies>

and into Import-Package section of  maven-jira-plugin of pom.xml

ru.matveev.alexey.tutorial.library.api,

Then we change MyServlet.java class to this one:

package ru.matveev.alexey.tutorial.obr.servlet;

import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ru.matveev.alexey.tutorial.library.api.LibraryService;

import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Named
public class MyServlet extends HttpServlet{
    private static final Logger log = LoggerFactory.getLogger(MyServlet.class);
    private final LibraryService libraryService;

    @Inject
    public MyServlet(@ComponentImport LibraryService libraryService) {
        this.libraryService = libraryService;
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        resp.setContentType("text/html");
        resp.getWriter().write("<html><body>" + libraryService.getLibraryMessage() + "</body></html>");
    }

}

That is all. We can launch jira with:

atlas-run

Jira will run on localhost:2990/jira. Login into Jira with admin:admin credentials and go to cog item -> add-ons -> manage add-ons. Then install obr file from the target folder of jira-obr plugin. After installation the manage add-ons section should look like this:

 obraddons.png

 Then you can launch the servlet with:

http://localhost:2990/jira/plugins/servlet/myservlet

 And you will see the text message from our first plugin:

obrserveltoutput.png

 

 

 

 

 

 

3 comments

Gonchik Tsymzhitov Community Champion Jan 24, 2018

Thanks for the article! 

Daniel Wester Community Champion Jan 24, 2018

This is awesome! You should submit it as a topic over at https://community.developers.atlassian.com so that the entire Development ecosystem can learn (or more importantly - it will become part of the resources available for developers).

Alexey Matveev Community Champion Jan 24, 2018

@Daniel Wester Done :)

Comment

Log in or Sign up to comment
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Aug 06, 2018 in Jira Service Desk

A is for Activate: Share your top Jira Service Desk onboarding tips for new users!

Hi, everyone! Molly here from the Jira Service Desk Product Marketing Team :).  In the spirit of this month's  august-challenge, we're sourcing stories of Jira Service Desk activation fro...

573 views 25 15
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you