Expand my Community achievements bar.

SOLVED

How do I reference an OSGi bundle in my Java class or POM file?

Avatar

Level 1

We are developing a proof of concept using AEM Correspondence Management and need to do a call to a REST service to retrieve data to prefill the correspondence letter templates. To do the POST we are trying to use com.sun.jersey.api.client which we have in our Maven repository and have also successfully built as a bundle in system/console/bundles according to: Adobe Experience Manager Help | Submitting Adobe Experience Manager form data to Java Sling Servlets under the section To create an OSGi bundle fragment that contains the org.json.simple.JSONObject class, perform these tasks.

However, although we reference this in our POM file, the imports are not resolving. Is there something we need to do within our POM file or somewhere else to reference the OSGi bundle we have installed?

        <dependency>

        <groupId>com.sun.jersey</groupId>

        <artifactId>jersey-client</artifactId>

        </dependency>

java.lang.Error: Unresolved compilation problems:

The import com.sun.jersey cannot be resolved

The import com.sun.jersey cannot be resolved

The import com.sun.jersey cannot be resolved

Client cannot be resolved to a type

Client cannot be resolved

WebResource cannot be resolved to a type

ClientResponse cannot be resolved to a type

ClientResponse cannot be resolved to a type

ClientResponse cannot be resolved to a type

Thank you!

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

The reason for the imports not getting resolved is when corresponding bundles are not available on AEM. Although you've added the dependency in POM that doesn't exist in AEM. So, either you'll have to explicitly installed "com.sun.jersey" jar in AEM web console or you can make this bundle as a part of your maven build so once you install the package, that bundle will be installed automatically.

To embedded bundle in maven project package build, go to ui.apps pom.xml and add dependency in <embeddeds></embeddeds> tags.
E.g.

Hope this helps!

View solution in original post

5 Replies

Avatar

Correct answer by
Community Advisor

The reason for the imports not getting resolved is when corresponding bundles are not available on AEM. Although you've added the dependency in POM that doesn't exist in AEM. So, either you'll have to explicitly installed "com.sun.jersey" jar in AEM web console or you can make this bundle as a part of your maven build so once you install the package, that bundle will be installed automatically.

To embedded bundle in maven project package build, go to ui.apps pom.xml and add dependency in <embeddeds></embeddeds> tags.
E.g.

Hope this helps!

Avatar

Community Advisor

I agree with the options but I guess the code snippet in incorrect

Avatar

Community Advisor

Hi Vivek,

I'm using the same code snippet in ui.apps pom.xml and it's working fine. In ui.apps pom.xml, there's already an embed of core project core bundle.

Use the <embedded></embedded> similar way and add groupId and artifect ID of dependency you want to include part of the package.

I'm not aware of the project pom structure differs with the different version of the archetype. 

Avatar

Level 10

This article works - been tested many times. What you are missing is you have not followed the instructions in the doc to wrap the JAR into an OSGI bundle and deploy to AEM.

Watch the video here where we step through all the steps and show it working:

Scott's Digital Community: Submitting Adobe Experience Manager form data to custom Sling Servlets

Avatar

Community Advisor

Wrapping JAR into as OSGi bundle is one option but not the only one. I find the maven way better as it is lesser of manual effort and I get the flexibility of using dependency private to the bundle or exposing it.

himanshusinghal​ : I was referring to the changes that one needs to make in maven-bunle-plugin.. Apache Felix - Apache Felix Maven Bundle Plugin (BND)  What you mentioned looks to be a different way of doing it.. Do you have sample code/github link that you can share. Good to know more ways of doing things