In this post, I will explain package version issues for AEM package dependencies. Here is the scenario where I got this issue:
In one of my project, I had to create a tool for AEM that supports AEM5.6.1 and AEM6.x versions. For some dependencies, these AEM version instances (CQ5.6.1, AEM6.0, AEM6.1, and AEM6.2) needed to use different version of dependencies.
If I used an older version of these dependencies then my bundle did not work in AEM6.2 and remains in installed state. If I used the higher version of these dependencies then my code was not working for the older versions of AEM (CQ5.6.1, AEM6.0, AEM6.1).
Versions available for these package dependencies in OSGi container are not same as required by my bundle.
For example, lets take the case of cq-commons dependency.
AEM6.2 supports 5.9.22 version of cq-commons dependency.
AEM6.1 supports 5.8.32 version of cq-commons dependency.
AEM6.0 supports 5.7.12 version of cq-commons dependency.
AEM5.6.1 supports 5.6.4 version of cq-commons dependency.
Because of these different versions some of my java packages (com.day.commons.jcr) were not getting resolved.
Here are the list of steps, I followed, to resolve this issue.
I have chosen the lowest version of available dependency i.e. 5.6.4 and added a dependency entry in the parent pom.xml file.
I defined a property in <properties> section in my parent pom.xml file as mentioned below.
Note: – you can use whatever tag name you want but to make it readable I used this name.
I have updated my <maven-bundle-plugin> as shown below-
Here I have used <DynamicImport-Package> tag. Now I have built my project for different AEM versions and it works fine for me in all AEM versions.
Here is the list of questions that may come in your mind:
Q1). What exactly <DynamicImport-Package> tag do?
It will add an entry in MANIFEST.MF file for the packages you mentioned in <bundle.dynamicImport.package> tag. MANIFEST.MF file entry is-
Q2). How bundle start working after using the <DynamicImport-Package> tag?
For answering this question-
First, we need to know about, How OSGi container works?
So, When you deploy your bundle into AEM OSGi container then first OSGi container checks that the available Java Packages versions are compatible with the versions of packages listed in Import-Package tag of your bundle MANIFEST.MF file. If version of Import-Package is not present then you will get an error as written below-
"com.day.cq.commons.jcr,version=[5.7,6) -- Cannot be resolved".
But if you defined these packages in <DynamicImport-Package> tag then OSGi container will not perform the versions checking for these packages and provides the available package definitions to your bundle at runtime.
i.e. using <DynamicImport-Package> is a kind of hack that short-circuits OSGi version checking process.
Note:- It is not recommended to use <DynamicImport-Package> with OSGi container.
Q3). Could we add multiple entries in <bundle.dynamicImport.package> tag?
Yes, you can add multiple packages using comma( “,”) delimiter. for ex.