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

This post illustrate the ways of embedding third party dependency in AEM maven project.
Steps are mentioned considering the project structure to be followed for AEMasCS while the same can be followed for classic versions too. 


Before we begin, I would like to explain the scenario of including a dependency in our AEM maven project. 
  • When we would like to make use of an API that is not already available in our project, we look for that in maven-central repository and specify the dependency in main pom.xml and desired module's pom.xml file. (Lets say, AWS S3 Java SDK V2 dependency)
  • Upon updating the project in the IDE, we would see our class files (making use of those APIs) are compiled/build successfully without any errors. 
  • Now, in the context of AEM/OSGi environment, when the same package and hence the bundle is deployed to AEM instance, dependencies that we added new might not be resolved/available in the OSGi as a bundle. Something like the below
  • This indicates that the added dependency is resolved in the context of Maven project and is not in the context of OSGi which paves way for the need of OSGi bundle of the desired dependency. 
    • While this is possible with our existing core module itself which makes use of bnd-maven-plugin (plugin in recent archetype versions for building core module), some dependencies which are in turn dependent on other dependencies(referred to as transitive dependencies) could not be resolvable as bundle in OSGi. Hence the need for maven-bundle-plugin. 
Creating an OSGi bundle out of third party non-osgi dependency using maven-bundle-plugin is explained in this blog post which is about building and deploying the OSGi bundle outside project code base as a standalone third party bundle. 

In order to do the same as part of single project code base which should be the case with Cloud Manager deployments in AEM as Cloud Service, have explained two possible ways that can be followed. 

1. If organization has their own Maven Artifact Repository. 

If the organization has publicly accessible Repository Manager (Like Nexus or Artifactory), then 

  • Upload the artifact (OSGi bundle of third party dependency) to Nexus
    • This can be separate activity outside cloud manager / outside the context of AEM project as a standalone separate maven project exclusively for creating and uploading the artifact to our own artifact repository.
  • Using the uploaded artifact in AEM project code base 
    • Register the org specific artifact repository in <repositories> section of main pom.xml file in desired build profile(Example: for local developement, we can make use of autoInstallPackage profile) For Cloud Manager deployment, we can make use of cmBuild profile. 
      • Note Network restricted artifact repository is not currently supported in AEMasCS/ Cloud Manager deployments. However, publicly available password protected artifact repository is supported when configured. Ways of configuring the same is detailed in Official doc
    • Embed the artifact(as available in Nexus by Step 1) in filevault-package-maven-plugin -> <embeddeds> entry of  all module + in <dependencies> section of ui.apps module

2. As a separate module in AEM project code base. 

If there is no org specific artifact repository, then we can have a dedicated module as part of the main project code base. 
  • Along the lines of suggested naming convention in Official doc, we can create module named common.core so that we can centralize all the third party dependencies in this module and use maven-bundle-plugin to build the same.
  • Artifact from this module is then embedded in all module
    • In AEMasCS project/ archetype project with version >= 24, OSGi bundle out of core module is embedded in all module as opposed to ui.apps module (Old project structure)
    •  Same way, we are embedding common.core artifact in all module as highlighted in screenshot.

Code:
Sample pom.xml file that creates OSGi bundle out of AWS S3 Java SDK V2 dependency. This can be used for both the methods except the fact that while using it in second method,
  • Update the groupId, artifactId according to the AEM project name
  • Include the <parent> element (We can copy it from existing core module)
  • Remove the <distributionManagement> entry
During Local development:
  • As part of local development, we should make sure that the OSGi bundle that we are creating out of third party dependency is exporting only the required API/packages (Explicitly list all the packages required in Export-Package entry and not "*")
  • We should also make sure that only our project bundles makes use of the custom bundle that we created. This can be verified by checking the "Importing Bundles" section of our custom OSGi bundle in Felix console.
  • In order to go about above two pointers, when we first develop,
    • Use the desired dependency in main and core/pom.xml and install the project (without OSGi bundle of the respective dependency) -> Our project bundle will be in installed state -> Look for dependencies that are in red/ not resolved -> While creating bundle, explicitly mention those and go from there on till we see the bundle in active state and hence the ultimate functionality (making use of desired dependency) is working.
Note :
Update (on 24 Dec 2021):
For including third party dependency using bnd-maven-plugin, please check -  https://myaemlearnings.blogspot.com/2021/12/embed-third-party-dependency-using-bnd.html

Comments

  1. This comment has been removed by the author.

    ReplyDelete
  2. This is a very detailed and informative guide for developers working with AEM! Thanks for sharing such practical insights. 😊

    Franchise for sale
    saw Palmetto Oil Manufacturer in Germany

    ReplyDelete
  3. The two methods explained here are crystal clear. It's great to see both organization-specific and standalone approaches covered.

    Staff Locker delhi
    overhead conveyor

    ReplyDelete
  4. This blog is a lifesaver for anyone struggling with third-party dependencies in AEM projects. Kudos! 🙌

    Soft Dirnks Franchise
    mezzanine floor faridabad

    ReplyDelete
  5. Embedding third-party dependencies as OSGi bundles can be tricky, but you've simplified it beautifully. Great job! 👏

    warehouse mezzanine floor delhi
    Mezzanine Floor manufactrurer in Delhi

    ReplyDelete
  6. The use of Maven Artifact Repositories is a clever solution. The step-by-step approach makes it very easy to follow. 👍

    Namkeen Distributorship
    Rosehip Oil Manufacturer in Korea

    ReplyDelete
  7. Loved how you pointed out the difference in project structures for archetype versions. Very thoughtful for backward compatibility! 🤩

    multi tier rack rudrapur
    Mezzanine floor in India

    ReplyDelete
  8. The code snippet link to GitHub is a nice touch. It adds more value to the post by providing practical examples. 🛠️

    Frozen Foods Distributorship
    Black Seed OIl Manufacturer in Korea

    ReplyDelete
  9. The note about network-restricted artifact repositories in AEMasCS is very insightful. It clears up potential confusion for developers. 💻

    office chair in delhi
    Screw feeder manufacturer

    ReplyDelete
  10. The explanation of transitive dependencies and their resolution in OSGi is spot on! It cleared up a lot of my doubts. 😊


    domino's franchise
    multi tier rack in chandigarh

    ReplyDelete
  11. Including a checklist for local development and verification in Felix Console is incredibly helpful. Thanks for this addition! ✅

    industrial storage rack in rudrapur
    Slotted Angle rack in India

    ReplyDelete
  12. The way you've detailed embedding the artifact in filevault-package-maven-plugin is commendable. It simplifies deployment complexities. 🌟

    invest in brands
    Argan Oil Manufacturer in Korea

    ReplyDelete
  13. Mentioning the support for password-protected repositories in Cloud Manager is a great tip. Good to know this is possible! 🔒

    Staff Locker noida
    PVC Conveyor belt Manufacturer

    ReplyDelete
  14. This blog is perfect for developers transitioning to AEMasCS. The clarity and depth are unmatched. Thanks for sharing! 🌍

    domino's franchise cost
    file compactor storage system ahmedabad

    ReplyDelete
  15. The focus on cloud manager compatibility is crucial, especially in today's cloud-centric development environments. Great job! ☁️

    warehouse storage rack in ghaziabad
    Slotted Angle rack manufacturer in Delhi

    ReplyDelete
  16. The suggested naming convention for the module common.core is a thoughtful addition. It keeps things organized and professional. 🗂️

    Franchise show Delhi
    Carrier Oil Saw PalMetto Manufacturer in Germany

    ReplyDelete
  17. The blog brilliantly balances between theory and practical examples. The screenshots and code samples add immense value. 📷

    cafeteria-furniture delhi
    Drag Chain Conveyor

    ReplyDelete
  18. It’s great to see the blog address both classic AEM and AEMasCS environments. Very inclusive approach! 👏

    franchise of hyderabadi biryani
    slotted angle rack ranchi

    ReplyDelete
  19. Excellent write-up! This will undoubtedly save developers countless hours of troubleshooting. Keep up the amazing work! 🙌

    industrial mezzanine floor delhi
    Industrial rack manufacturer in India

    ReplyDelete

Post a Comment

Popular posts from this blog

OSGI Factory Configuration implementation

Embed Third party dependency using bnd-maven-plugin