May 26, 2020
Estimated Post Reading Time ~

Maven Dependencies Version Issue in AEM

In this post, I will explain package version issues for AEM package dependencies. Here is the scenario where I got this issue:

[av_heading heading=’Issue Description’ tag=’h2′ style=” size=” subheading_active=” subheading_size=’15’ padding=’10’ color=” custom_font=”][/av_heading]
In one of my projects, 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 versions 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).

[av_heading heading=’Underlying Reason’ tag=’h2′ style=” size=” subheading_active=” subheading_size=’15’ padding=’10’ color=” custom_font=”][/av_heading]
Versions available for these package dependencies in OSGi containers are not the same as required by my bundle.
For example, lets take the case of cq-commons dependency.

<dependency>
    <groupId>com.day.cq</groupId>
    <artifactId>cq-commons</artifactId>
    <version>x.x.x</version>
    <scope>provided</scope>
</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.

[av_heading heading=’Solution’ tag=’h2′ style=” size=” subheading_active=” subheading_size=’15’ padding=’10’ color=” custom_font=”][/av_heading]

Here is the list of steps, I followed, to resolve this issue.

[av_heading heading=’Step 1′ tag=’h3′ style=” size=” subheading_active=” subheading_size=’15’ padding=’10’ color=” custom_font=”][/av_heading]
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.

<dependency>
   <groupId>com.day.cq</groupId>
   <artifactId>cq-commons</artifactId>
   <version>5.6.4</version>
   <scope>provided</scope>
</dependency>
[av_heading heading=’Step 2′ tag=’h3′ style=” size=” subheading_active=” subheading_size=’15’ padding=’10’ color=” custom_font=”][/av_heading]
I defined a property in <properties> section in my parent pom.xml file as mentioned below.

<bundle.dynamicImport.package>com.day.cq.commons.jcr</bundle.dynamicImport.package>
Note: – you can use whatever tag name you want but to make it readable I used this name.

[av_heading heading=’Step 3′ tag=’h3′ style=” size=” subheading_active=” subheading_size=’15’ padding=’10’ color=” custom_font=”][/av_heading]
I have updated my <maven-bundle-plugin> as shown below-

<plugin>
   <groupId>org.apache.felix</groupId>
   <artifactId>maven-bundle-plugin</artifactId>
   <version>2.3.7</version>
   <inherited>true</inherited>
   <extensions>true</extensions>
   <configuration>
       <instructions>
           <DynamicImport-Package>${bundle.dynamicImport.package}</DynamicImport-Package>
       </instructions>
   </configuration>
</plugin>
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.

[av_hr class=’default’ height=’50’ shadow=’no-shadow’ position=’center’ custom_border=’av-border-thin’ custom_width=’50px’ custom_border_color=” custom_margin_top=’30px’ custom_margin_bottom=’30px’ icon_select=’yes’ custom_icon_color=” icon=’ue808′ font=’entypo-fontello’]

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-

DynamicImport-Package: com.day.cq.commons.jcr
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.

<bundle.dynamicImport.package>
   com.day.cq.replication,
   com.day.cq.commons.jcr
</bundle.dynamicImport.package>


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.