Monday, April 30, 2012

How to Integrate 3rd party Jar file in CQ / WEM

Use Case: You have a non osgi jar file that you want to integrate in CQ as a bundle. You want to create fragment bundle (See option 4)

Solution: There are various way you can do this, Here is some options.

Option 1:

jar cvfm <Your bundle name> manifest.txt <Non OSGI Jar file>

manifest.txt file will look like this

Manifest-Version: 1.0
Created-By: yogesh
Bundle-ManifestVersion: 2
Bundle-Name: <Your Bundle name>
Bundle-Description: JTMB bundle for dvdRental
Bundle-Version: 4.4.0
Bundle-ClassPath: .,<Your jar file>
Bundle-SymbolicName: <Bundle Symbolic name>
Export-Package: <Package you want to export, Just export selective package>
Import-Package: <Package this jar file is importing from others, You can put * as well>

After bundle is created you can put them in /apps/<your app>/install folder, So that they can be picked by Felix. Once in Install Folder you will see them in felix console

Option 2: (Only till CQ5.4)

Other option is use sling boot delegation to add third party jar.

Option 3: 

Using BND tool

1) Install bnd.jar
2) Use command java -jar bnd.jar wrap {YourJarfile}.jar
3) Rename .bar file to {file-name}-bundle.jar
4) Put it in install folder or install using felix console

Create OSGI bundle using BND with additional property

java -jar bnd.jar wrap -properties bundle.bnd {YourJarfile}.jar
bundle.bnd will look something like this
Export-Package: <Package you want to export>
Private-Package: <Package you want to import>
Bundle-Version: 1
Bundle-Classpath: .,{yourjarfile}

More information of available options here

Option 4:

Using maven-bundle-plugin

1) Put your Jar file under /libs folder (using Web Dav) of your project and include it in project menifest or pom.xml

If you are using CRXDE then your bundle menifest will look like this. In this example, I assume that /libs folder is created at same level of your menifest file.

Export-Package: <Package you want to export, Use * for everything but not recommended>
Import-Package: *;resolution:=optional
Private-Package: <If you want>
Dynamic-Import: *
Embed-Dependency: *;scope=compile|runtime
Embed-Directory: /libs
Embed-Transitive: true

Your pom.xml will look like this

<!-- You can use extension true to create fragment bundle -->

                                               .... All non OSGI jar file
                                        <Fragment-Host>system.bundle; extension:=framework</Fragment-Host>
                                           ... Your comma seperated Export Package

Some more useful doc

A good example is

Feel free to let me know if missing something.

1 comment:

  1. For users of Eclipse the bndtools plugin is a great way to take a normal JAR file and convert it to an OSGi bundle.

    First go to the bndtools website and follow the instructions for installation:

    Once that is installed here are the steps to create an OSGi bundle from a JAR file:

    1) In Eclipse go to File->New->Other...
    2) Filter the list on "osgi"
    3) Select the option "Wrap JAR as OSGi Bundle Project" and Click Next
    4) It might ask you to "Create a configuration project" select that and click Next and choose Standard or Lightweight... then click Finish
    5) Next it will show a dialog where you can Add your JAR. Click Add External and browse to your JAR file. Click Next
    6) Select the packages you want to export (I usually Add All ->)
    7) At this point you can either finish or browse the rest of the wizard dialogs to configure your project

    That's it. It has now been created for you. If you expand your project in the Package Explorer you will see a folder was created called "generated". In it there is a .jar file. That is your OSGi bundle. I generally then drag and drop that file from Eclipse to my Desktop.