Welcome to part 3, in this part we will get familiar with Package Import and Export in OSGI bundles
After reading this article you should be able to
- Describe, how the package import and export is organised in OSGI bundles.
- Embed an external JAR file into a bundle.
Maven Bundle Plugin
Let’s see how Maven plugin generates those properties.
By default the plugin makes all the packages in the bundle exported with 2 exceptions. The packages whose name are “impl” or “internal”, those packages are not exported in remain private to the bundle.
For the exported packages it determines the dependencies and places them in uses element as we see in the LoggingFilter.java
To determine the version of the exported package it uses packageInfo class If the packageInfo is not present it uses the maven project version as the package version. The plugin formats the version in the major, minor, micro format.
The plugin determines the imported packages by analysing the class imports and manifest files of the libraries. The version ranges created from the version number truncated to the minor version inclusive to the next major version number exclusive.
Bundle Customization in the Plugin
Bundle description can be customized using configuration instructions:
<configuration>
<instructions>
<Export-Package>
com.training.osgi.core.models;version="2.0.0",
!com.training.osgi.core, com.training.osgi.core.*
</Export-Package>
<Private-Package>${bundle.private}</Private-Package>
<Import-Package>javax.inject;version=0.0.0,*</Import-Package>
</instructions>
</configuration>
Sometimes developers needs to override the default logic and customise bundle properties manually. That can be done using the plugin configuration instructions, we are currently on the package level so we are interesting in the instructions that exports and import packages. The names of the instructions are pretty much self explanatory-
<Export-Package> – is used to override the exported packages- the list of the packages their properties such as used imports and versions. Also one can use the exclamation mark to instruct not to import the package and one can use wildcard to export several packages.
<Private-Package> instruction is used to list the packages that are not exported and, note that the maven property can be used to get list of the private packages.
<Import-Packages> is for overriding the imported packages – the instruction that you see is added when one creates an AEM project from a maven artifact and what it says is import javax.inject version 0.0.0 i.e. any available version and declare the rest of the packages that * declare the rest of the packages using the default logic.
Useful Link: https://felix.apache.org/documentation/subprojects/apache-felix-maven-bundle-plugin-bnd.html
Maven Version at the Same Time
In AEM,
- It is not possible to have multiple versions of the same bundle
- It is possible to have multiple versions of the same package
Bundles and Libraries
What we discussed so far, it was about packaging the project classes in bundles, it is sometimes needed to deploy a third party library as a bundle, how do we do that?
First- One should make sure that the library is not already present in the AEM that can be done using the dependency finder. There can also be a ready to use bundle with the library already available in the internet. If it is not available the solution will be to embbed that is place the library in your bundle .
There are two options here
- you either embbed the library to use inside the bundle internally, in that case use the embbed instructions as shown in the above picture,
- or you may want to export the library to other modules- in that case you need to use the embbed instructions but also add _exportcontents instruction and list the packages to export probably with the proper version numbers.
Summary• Maven Bundle Plugin automatically determines the packages that are exported and imported by the bundle
• One can override the default build logic using Maven Bundle
Plugin’s configuration instructions
• It is possible to export multiple versions of the same package in
AEM
• One can use “Package Dependencies” (depfinder) console to check whether a specific package is exported and by what bundle
• It is possible to embed third-party libraries in bundles
Hope you enjoyed this article, I would like to suggest, Kindly watch video below for practical session, Thank you for your attention! In the next part we will get familiar with Bundle Life Cycle. Click here to visit page!