Create/build and install OSGi bundle of third party APIs/dependency using maven plugins

This post details the steps to create and deploy OSGI bundle out of third party API/dependency using relevant plugins in simple maven project.

We would have been in situation where OSGi bundle created out of third party jar is added/available in our instance and our project code base using the third party bundle being in active state. However at the time of execution, ClassNotFound or NoClassDefFound error pops up.

This is the case when the dependencies of our desired third party API is not included/available when it is added as an OSGi bundle.

We can overcome this by creating the bundle using maven-bundle-plugin which has option to include

  • direct(desired third party dependency - highlighted in red in below screenshot - 
    • * -  to include all the dependency entry in the pom.xml file or 
    • artifactId name if we would like to mention explicitly 
    • Note : There are several other possible options documented for <Embed-Dependency> in official plugin page (Mentioned the link at the end in Reference section)
  • transitive dependencies (dependencies which our third party API is dependent on - highlighted in green in below screenshot is responsible for the same)


With this plugin information in the <build> tag + third party dependency of our interest in <dependencies> section along with details like group id, artifact id in pom.xml file will help create OSGi bundle using the command - mvn package or mvn clean package

Next, to install the created bundle directly to felix console, we can make use of a plugin named sling-maven-plugin which has properties to specify

  • AEM instance felix console URL(as highlighted in green in below screenshot) + 
  • username and password details(mentioned in <properties> as <sling.user> and <sling.password> respectively) 
  • Note: One of its goal named install is used for installing the bundle to felix console. Official plugin page is mentioned at the end in Reference section.

Updated command to use : mvn clean package install


To demonstrate with specific example, I would like to pick Box API as third party dependency which was recently raised in our community forum. Link to the thread for reference - https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/integrate-aem-with-box-com-to-store-completed-forms/qaq-p/396609

Below highlighted dependency information from Maven central for Box Java SDK is used.


Complete pom.xml file is added to my GitHub repo - https://github.com/Viji1304/aemlearnings-samples/tree/master/BOXSDK

Steps to try in your local:
  • Download the folder from above location
  • Update the instance port, user name and password per your local instance in pom.xml file and with dependency of your interest. 
  • Execute mvn clean package install  from cmd by being in pom.xml file location
  • Look for the bundle in target folder and in your local instance felix console.
As a standalone bundle: 
Based on our real time project set up + per the functionality expected out of 3rd party APIs, 

  • We can choose to have this as a separate project and hence separate bundle if you have many projects/bundle which might make use of in future or
  • If you have common code base which all other projects are making use of, this can be included as part of the same.
As part of project code base:
  • If your project's core module makes use of maven-bundle-plugin, it will work on the similar lines as explained above where desired third party dependency is added to main pom.xml and core/pom.xml + highlighted instructions in screenshot above of maven-bundle-plugin is used.
  • In recent AEM Maven archetype version, bnd-maven-plugin is used to build OSGi bundle out of core module wherein instruction named -conditionalpackage is suggested to use for the similar purpose. However, I couldn't gain success in this case for one of the third party API that I tried in my local, will update once it works.
Reference:

Comments

Popular posts from this blog

Embedding Third party dependency/OSGi bundle in AEM application hosted in AEMasCS

OSGI Factory Configuration implementation

Creation of Template Types for Editable templates