April 27, 2020
Estimated Post Reading Time ~

com.google.gson,version=[2.2,3) -- Cannot be resolved in bundle

I was able compile and deploy the bundle successfully with gson dependency but the bundle is not getting started with the error "com.google.gson,version=[2.2,3) -- Cannot be resolved in bundle" in AEM.

The root cause of the problem is the gson jar is not available in the server for bundle access.

This issue can be resolved by following any of the below approaches.

1. Export the gson package - com.google.gson.*
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
<scope>compile</scope>
</dependency>

<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-Activator>com.sample.activator.Activator</Bundle-Activator>
<Import-Package>*,com.sample.*</Import-Package>
<Export-Package>com.sample.*,com.google.gson.*</Export-Package>
<Bundle-SymbolicName>com.sample</Bundle-SymbolicName>
<Bundle-Vendor>Sample</Bundle-Vendor>
<Bundle-Category>Sample</Bundle-Category>
<Embed-Directory>dependencies</Embed-Directory>
<Embed-Transitive>true</Embed-Transitive>
</instructions>
</configuration>

</plugin>


2. Add the gson jar in the bundle classpath
specify the gson dependency scpes as compile or runtime(compile is the default scope)

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
<scope>compile</scope>
</dependency


The gson jar will be included in the bundle classpath.

The dependencies with what are all the scopes are included in the bundle classpath is configured in maven-bundle-plugin

<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.3.7</version>
<configuration>
<instructions>
<Embed-Dependency>*;scope=compile|runtime</Embed-Dependency>
<Embed-Directory>OSGI-INF/lib</Embed-Directory>
<Embed-Transitive>true</Embed-Transitive>
</instructions>
</configuration>
</plugin>




3. Install the gson jar as bundle from felix console
The above two approaches the gson jar will be only available for the bundle but in this approach, the jar will be available for all the bundles.

Go to http://localhost:4502/system/console/bundles
Click on Install/Update
Select Start Bundle and choose the gson jar.
Click on Install or Update.


By aem4beginner

No comments:

Post a Comment

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