Consider a scenario where you need to use a non-OSGi jar in your OSGi project. Most libraries are not OSGi bundles and we should repackage all them with OGSi headers to be loaded into the OSGi container. There are several ways to help you with these non-OSGi dependencies:

  • Embed these jar files into your OSGi bundle
  • Wrap these jar files within one OSGi bundle

For instance, you need to utilize fastjson library to serialize/deserialize json object in your OSGi bundle.

Embed these jar files into your OSGi bundle
<plugin>
    <groupId>org.apache.felix</groupId>
    <artifactId>maven-bundle-plugin</artifactId>
    <version>2.4.0</version>
    <extensions>true</extensions>
    <configuration>
        <instructions>
            <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
            <Bundle-Version>${project.version}</Bundle-Version>
            <Bundle-Activator>com.example.demo.Activator</Bundle-Activator>
            <Export-Package>
                com.example.demo*;version=${project.version}
            </Export-Package>
            <Import-Package>
                !org.springframework.*,
                *
            </Import-Package>
            <Embed-Dependency>fastjson;scope=compile|runtime|system;inline=true</Embed-Dependency>
        </instructions>
    </configuration>
</plugin>

If you want a dependency inlined instead of embedded add the inline=true.

inline=true inline=true

Wrap these jar files within one OSGi bundle
Karaf supports the wrap:protocol execution, so it allows for directly deploying third party dependencies into OSGi container:
install -s wrap:mvn:com.alibaba/fastjson/1.1.37
You can also create a wrap bundle for a third party dependency. This bundle is simply a Maven POM that shades an existing jar and package into a jar bundle.
<plugin>
    <groupId>org.apache.felix</groupId>
    <artifactId>maven-bundle-plugin</artifactId>
    <version>2.4.0</version>
    <extensions>true</extensions>
    <configuration>
        <instructions>
            <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
            <Bundle-Version>${project.version}</Bundle-Version>
               <Import-Package>!org.springframework.*,*</Import-Package>
            <Private-Package>!*</Private-Package>
            <Embed-Dependency>*;scope=compile;type=!pom;inline=true</Embed-Dependency>
            <Export-Package>com.alibaba.fastjson*</Export-Package>                  
        </instructions>       
    </configuration>
</plugin>

References
http://karaf.apache.org/manual/latest-2.3.x/developers-guide/creating-bundles.html
http://felix.apache.org/site/apache-felix-maven-bundle-plugin-bnd.html
http://baptiste-wicht.com/posts/2010/03/bundle-non-osgi-dependencies-maven.html
http://maksim.sorokin.dk/it/2011/08/09/maven-apache-felix-strategy-to-handle-non-osgi-dependencies/

Integrate non-OSGi Dependencies的更多相关文章

  1. Liferay7 BPM门户开发之38: OSGi模块化Bndtools、Maven、Gradle开发构建入门

    前言 OSGi是目前动态模块系统的事实上的工业标准,它适用于任何需要模块化.面向服务.面向组件的应用程序.Eclipse如此庞大和复杂的插件体系,就是基于OSGi.Liferay也是基于OSGi.OS ...

  2. maven-bundle-plugin插件, 用maven构建基于osgi的web应用

    maven-bundle-plugin 2.4.0以下版本导出META-INF中的内容到MANIFEST.MF中 今天终于把maven-bundle-plugin不能导出META-INF中的内容到Ex ...

  3. osgi实战学习之路:3. osgi分层概念及相互合作demo

    源码下载 分层: modual: 主要作用于包级管理与共享代码 lifecycle: 主要作用于执行期间的模块管理与訪问osgi底层框架 service: 主要作用于多模块之间的相互通信 demo: ...

  4. OSGI企业应用开发(九)整合Spring和Mybatis框架(二)

    上篇文章中,我们完成了在OSGI应用中整合Spring和Mybatis框架的准备工作,本节我们继续Spring和Mybatis框架的整合. 一.解决OSGI整合Spring中的Placeholder问 ...

  5. OSGI企业应用开发(七)细说Blueprint & Gemini Blueprint(二)

    上篇文章介绍了标准的Blueprint 规范与 Gemini Blueprint如何自定义Bean配置文件路径,本文接着上篇文章继续介绍Blueprint的使用. 一.Bean的配置 前面提到过,Ge ...

  6. OSGI企业应用开发(六)细说Blueprint & Gemini Blueprint(一)

    上篇文章介绍了如何使用Blueprint將Spring框架整合到OSGI应用的Bundle中,从上篇文章中我们大概了解了Blueprint与Gemini Blueprint的关系,简单的说,Bluep ...

  7. OSGI企业应用开发(五)使用Blueprint整合Spring框架(二)

    上篇文章中,我们开发了一个自定义的Bundle,接着从网络中下载到Spring和Blueprint的Bundle,然后复制到DynamicRuntime项目下. 需要注意的是,这些Bundle并不能在 ...

  8. OSGI企业应用开发(四)使用Blueprint整合Spring框架(一)

    上篇文章中介绍了如何使用独立的Equinox发行包搭建OSGI运行环境,而不是依赖与具体的Eclipse基础开发工具,本文开始介绍如何使用Blueprint將Spring框架整合到OSGI中. 一.开 ...

  9. OSGI企业应用开发(二)Eclipse中搭建Felix运行环境

    上篇文章介绍了什么是OSGI以及使用OSGI构建应用的优点,接着介绍了两款常用的OSGI实现,分别为Apache Felix和Equinox,接下来开始介绍如何在Eclipse中使用Apache Fe ...

随机推荐

  1. Oracle 添加字段,并自增

    第一步:alter table TOWN add ID int 第二步:Update TOWN set id=rownum; Commit;

  2. Magento1.9批量修改产品 Attribute Set

    今天修改产品时遇到这样一个需求:重新设置产品的 Attribute Set,使用的是Magento1.9系统,Magento提供这样一个插件 Flagbit Change Attribute Set: ...

  3. Android自定义控件

    开发自定义控件的步骤: 1.了解View的工作原理  2. 编写继承自View的子类 3. 为自定义View类增加属性  4. 绘制控件  5. 响应用户消息  6 .自定义回调函数    一.Vie ...

  4. asp.net 琐记

    Page的AutoEventWireup作用是是否引发PreInit Load PreRender Unload几个页面处理流程事件 和控件的事件处理函数无关

  5. HTML5 ---localStorage储存实例

     <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title> ...

  6. java_method_数据去重

    /** * 判断list本身是否有重复数据和标题 * * @param list * @return newList 返回没有重复数据的list */ public List<String[]& ...

  7. flash中htmlText样式

    赋值htmlText时,htmlText中指定了样式的按指定样式显示,没有指定的按该文本的默认样式显示. PS:flash软件拖出来的文本,在赋值htmlText的时候该文本原有样式会失效,而new出 ...

  8. opencv单目摄像机标定(二)

    // 引入实际标定板方格宽度的标定程序 #include <string> #include <iostream> #include <cv.h> #include ...

  9. submit 读取mb52数据

    方法一: data:list_tab type table of abaplist.   data:vlist(300) type c occurs 0 with header line. submi ...

  10. sqlserver索引与查询优化

    此文为转载,仅做保存使用,出处:http://www.cr173.com/html/8688_all.html 在数据库存优化设计中往往会提到索引,这编文章就来详细的说明一下在 SQL SERVER ...