Sharing common libraries across v2 plugins

We have already explored creating both v1 and v2 plugins throughout this book. One major difference between v1 and v2 plugins is that v1 plugins have access to all the libraries and classes available in the application class path, whereas v2 plugins can't access them.

For example, v1 plugins can access some common utility classes by dropping the JAR file with those classes in the WEB-INF/lib file or adding those classes under WEB-INF/classes. However, that won't work with v2 plugins as they need the JAR files embedded with them under META-INF/lib or the classes embedded in them. How will we handle this scenario when there is a utility class that we need to share across a few v2 plugins? Should we embed the class in all the plugins? The answer is no, and in this recipe, we will see how we can share those utility classes across v2 plugins by creating an OSGi bundle.

Getting ready

Create a skeleton plugin using Atlassian Plugin SDK.

How to do it...

Let us assume we have a Number utility class that does summation and multiplication of integer numbers. What should we do if we want to make this class available in all the v2 plugins? The following are the steps required:

  1. Create the Utility class under the correct package:
            package com.jtricks.utilities;
            public class NumberUtility { 
                public static int add(int x, int y) {
                  return x + y;
                }
            }
  2. Export the packages that need to be shared so that they are visible to other v2 plugins. This step is very important.

    This is done in the instructions section in the maven-jira-plugin definition in the plugin pom.xml. This element allows various bundle instructions and the following are the two elements we need.

    a. Export-Package: To export selected packages from the plugin to be shared across other plugins.

    b. Import-Package: To import only selected packages into a plugin. By default, it imports all the exported packages from other plugins.

    In our example, the package is com.jtricks.utilities, and we can export it using the Export-Package section, as shown here:

            <build>
             <plugins>
               <plugin>
                 <groupId>com.atlassian.maven.plugins</groupId>
                    <artifactId>maven-jira-plugin</artifactId>
                     .... 
                      <configuration>
                      <productVersion>${jira.version}</productVersion>
                      <instructions>
                        <Atlassian-Plugin-Key>
                          ${atlassian.plugin.key}
                        </Atlassian-Plugin-Key>
                        <!-- Add package to export here -->
                        <Export-Package> 
                          com.jtricks.utilities, 
                        </Export-Package> 
                          ...
                        <Spring-Context>*</Spring-Context>
                      </instructions>
                    </configuration>
                  </plugin>
                  .... 
                </plugins>
              </build>
  3. Package the plugin and deploy it as a v2 plugin.

With that, the plugin is set up to expose our utility class to other v2 plugins.

It is also possible to export only selected versions and not export certain packages. More details on the usage of bundle instructions can be found at http://www.aqute.biz/Bnd/Bnd.

When developing other plugins, we need to add the above plugin as a dependency in the pom.xml with scope as provided.

<dependency>
   <groupId>com.jtricks</groupId>
   <artifactId>number-utility</artifactId>
   <version>1.0</version>
   <scope>provided</scope>
</dependency> 

Optionally, we can use the Import-Package element in the bundle instructions to import the package we exported earlier. By default, it will be imported anyway, and hence this step can be omitted. However, it can be useful for when you want to import only selected packages or make the import mandatory, and so on. Again, the details can be found at http://www.aqute.biz/Bnd/Bnd.

Now, the method add can be invoked as if the class is within the same plugin. For example, a webwork action class can use the method as follows:

package com.jtricks.jira.webwork;
 import com.atlassian.jira.web.action.JiraWebActionSupport;
 import com.jtricks.utilities.NumberUtility;
 public class MathAction extends JiraWebActionSupport {
     private int sum;
     @Override
     public String doDefault() throws Exception {
         sum = NumberUtility.add(2, 3);
         return super.doDefault();
     }
     public int getSum() {
         return sum;
     }
 } 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.167.114