It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

 

 

 

 

 

 

16 comments

Thanks for the article! 

Daniel Wester Community Leader 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).

Hi @Alexey Matveev _cPrime_ 

I downloaded your project (following your address https://bitbucket.org/alex1mmm/jira-library/src/master/ and https://bitbucket.org/alex1mmm/jira-obr/src/master/ )

Change the config for <systemPath> then I try to run project
But somehow I got the error " The plugin cannot enable"
Screen Shot 2020-05-19 at 10.06.55.png

 

Try to enable it I got the log 


"Cannot determine required plugins, cannot resolve bundle 'ru.matveev.alexey.tutorial.obr.jira-obr'
2020-05-19 10:00:45,573 http-nio-2990-exec-10 INFO admin 600x234x1 ubp5d 0:0:0:0:0:0:0:1 /rest/plugins/1.0/ru.matveev.alexey.tutorial.obr.jira-obr-key [c.a.plugin.util.WaitUntil] Plugins that have yet to be enabled: (1): [ru.matveev.alexey.tutorial.obr.jira-obr], 300 seconds remaining
2020-05-19 10:00:46,576 http-nio-2990-exec-10 INFO admin 600x234x1 ubp5d 0:0:0:0:0:0:0:1 /rest/plugins/1.0/ru.matveev.alexey.tutorial.obr.jira-obr-key [c.a.plugin.util.WaitUntil] Plugins that have yet to be enabled: (1): [ru.matveev.alexey.tutorial.obr.jira-obr], 298 seconds remaining
2020-05-19 10:00:47,581 http-nio-2990-exec-10 INFO admin 600x234x1 ubp5d 0:0:0:0:0:0:0:1 /rest/plugins/1.0/ru.matveev.alexey.tutorial.obr.jira-obr-key [c.a.plugin.util.WaitUntil] Plugins that have yet to be enabled: (1): [ru.matveev.alexey.tutorial.obr.jira-obr], 297 seconds remaining
2020-05-19 10:00:48,583 http-nio-2990-exec-10 INFO admin 600x234x1 ubp5d 0:0:0:0:0:0:0:1 /rest/plugins/1.0/ru.matveev.alexey.tutorial.obr.jira-obr-key [c.a.plugin.util.WaitUntil] Plugins that have yet to be enabled: (1): [ru.matveev.alexey.tutorial.obr.jira-obr], 296 seconds remaining ....

2020-05-19 10:05:45,431 http-nio-2990-exec-10 INFO admin 600x234x1 ubp5d 0:0:0:0:0:0:0:1 /rest/plugins/1.0/ru.matveev.alexey.tutorial.obr.jira-obr-key [c.a.plugin.util.WaitUntil] Plugins that have yet to be enabled: (1): [ru.matveev.alexey.tutorial.obr.jira-obr], 0 seconds remaining
2020-05-19 10:05:46,641 http-nio-2990-exec-10 INFO admin 600x234x1 ubp5d 0:0:0:0:0:0:0:1 /rest/plugins/1.0/ru.matveev.alexey.tutorial.obr.jira-obr-key [c.a.plugin.manager.DefaultPluginManager] Disabling ru.matveev.alexey.tutorial.obr.jira-obr
2020-05-19 10:05:48,042 http-nio-2990-exec-10 DEBUG admin 600x234x1 ubp5d 0:0:0:0:0:0:0:1 /rest/plugins/1.0/ru.matveev.alexey.tutorial.obr.jira-obr-key [c.a.activeobjects.osgi.ActiveObjectsServiceFactory] onPluginDisabledEvent removing delegate for [ru.matveev.alexey.tutorial.obr.jira-obr]
2020-05-19 10:05:48,078 http-nio-2990-exec-10 ERROR admin 600x234x1 ubp5d 0:0:0:0:0:0:0:1 /rest/plugins/1.0/ru.matveev.alexey.tutorial.obr.jira-obr-key [c.a.plugin.manager.PluginEnabler] Unable to start the following plugins due to timeout while waiting for plugin to enable: ru.matveev.alexey.tutorial.obr.jira-obr "


My atlas-version is: 
Screen Shot 2020-05-19 at 10.14.46.png

Do you have any advice? 
Looking for your response. Thank you in advance.

Tien Dang

@Dang_Thi_Thuy_Tien 

Try to change this line in the pom.xml for jira-obr:

<Import-Package>ru.matveev.alexey.tutorial.library.api,org.springframework.osgi.*;resolution:="optional", org.eclipse.gemini.blueprint.*;resolution:="optional", *</Import-Package> 

to

<Import-Package>ru.matveev.alexey.tutorial.library.api,org.springframework.osgi.*;resolution:="optional", org.eclipse.gemini.blueprint.*;resolution:="optional", *;resolution:="optional"</Import-Package> 

and in jira-library

 <Import-Package> org.springframework.osgi.*;resolution:="optional", org.eclipse.gemini.blueprint.*;resolution:="optional", * </Import-Package>

to

 <Import-Package> org.springframework.osgi.*;resolution:="optional", org.eclipse.gemini.blueprint.*;resolution:="optional", *;resolution:="optional" </Import-Package>

Hi @Alexey Matveev _cPrime_ 

Thank for your quick response.
I change the config like your suggestion then clean and run for both projects again. But I got the same issue.

@Dang_Thi_Thuy_Tien 

unzip the jira-obr file and make sure that you have the jira-library.jar in the dependencies folder in the jar file.

@Alexey Matveev _cPrime_  
I couldn't file any jira-library.jar inside the jira-obr-1.0.0-SNAPSHOT.jar after unzipping it
Screen Shot 2020-05-19 at 10.44.05.png

@Dang_Thi_Thuy_Tien 

Sorry, Could you unpack the jira-obr.obr file? The extension of the file must be obr. If you can find the jira-library.jar inside, then go to Manage apps and upload the obr file.

Hi @Alexey Matveev _cPrime_ 
Yes, Inside the file.obr have the jira-library.jar. I try to upload file obr manually and plugin can run. Really great!

But, that means we have to upload the file manually?
Do you have any idea just using only the jar file. Can I push the jira-library.jar inside the jar file?

Thank you so much.

@Dang_Thi_Thuy_Tien 

No, you can not add it to the jar file. 

What do you mean you should upload it manually? If you mean you need to upload it manually while developing an app, then you should upload it only once. Then you can package your plugin and your changes will be applied to your dev instance.

Hi, @Alexey Matveev _cPrime_ 

Yes, You are right. I should upload the obr file only once during the development. 

My goal is building a bridge like this https://community.developer.atlassian.com/t/looking-for-bridge-api-lib-for-compatibility-jira-api-version-7-x-and-8-x-similar-to-jira-cross-compatibility-lib-bridge-api/38338

I have to create a couple of own dependencies. Then inject to the project (like your tutorial)
And we want to package it into 1 jar file. -> one plugin 

Do you have any advice for me? Sorry to make annoy you.


Thank you.

@Dang_Thi_Thuy_Tien 

First of all you need to make sure that all your dependencies are bundles which can be understood by OSGI. If not you can do one of the following:

1. make your dependencies as OSGI bundles

2. upack your libraries in your jar file (make compile scope for these libraries in the pom.xml)

3. add to META-INF/lib folder

Hi, @Alexey Matveev _cPrime_  

I try to follow your suggestion:

1. Change the <packaging> in lib to jar.

<packaging>jar</packaging>

 2. Copy the lib.jar file in local repository
 3. Change the dependency <scope> 

<scope>compile</scope>

4. Copy the lib.jar file into META-INF/lib folder

-> I got the same issue :( 

@Dang_Thi_Thuy_Tien You should delete 

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

form the pom.xml

Hi, @Alexey Matveev _cPrime_ 
Yes, I did. But it got the same issue
I try to change the <Export-package> in jira-obr project like this:

<Export-Package>ru.matveev.alexey.tutorial.obr.api,ru.matveev.alexey.tutorial.library.api,</Export-Package>

 It works for me.

Thank you so much

Comment

Log in or Sign up to comment
TAGS

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you