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:

http://dev.day.com/content/kb/home/cq5/Development/ApacheFelix/ConvertAJarIntoOsgiBundle.html

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 http://dev.day.com/content/kb/home/cq5/Development/SlingBootdelegation.html to add third party jar.


Option 3: 

Using BND tool

http://help.adobe.com/en_US/enterpriseplatform/10.0/AEPDeveloperGuide/WS562be5d616a63050-c62634713136615f38-8000.html

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
version=<Version>
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 http://fusesource.com/docs/esb/4.2/deploy_osgi/DeployJar-Convert.html

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

<build>
        <plugins>
        <plugin>
                        <groupId>org.apache.felix</groupId>
                        <artifactId>maven-bundle-plugin</artifactId>
                        <version>2.0.1</version>
                        <extensions>true</extensions>
<!-- You can use extension true to create fragment bundle -->

                <configuration>
                 
                    <instructions>
                                     <Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
                                        <Bundle-Version>${project.version}</Bundle-Version>
                                        <Include-Resource>lib=${basedir}/lib</Include-Resource>
                                        <Bundle-ClassPath>
                                                .,
                                               .... All non OSGI jar file
                                        </Bundle-ClassPath>
                                        <Fragment-Host>system.bundle; extension:=framework</Fragment-Host>
                                        <Export-Package>
                                           ... Your comma seperated Export Package
                                        </Export-Package>
                                        <Import-Package>!*</Import-Package>
                                        <Embed-Dependency>*;scope=compile|runtime</Embed-Dependency>
                                        <Embed-Transitive>true</Embed-Transitive>
                        </instructions>
                        </configuration>
                </plugin>
        </plugins>
    </build>

Some more useful doc

http://www.cqblueprints.com/xwiki/bin/view/Blue+Prints/Deploying+3rd+Party+Libraries

http://felix.apache.org/site/apache-felix-maven-bundle-plugin-bnd.html


A good example is http://www.wemblog.com/2012/03/how-to-integrate-soap-web-service.html

Feel free to let me know if missing something.

3 comments:

  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: http://bndtools.org/installation.html

    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.

    ReplyDelete