使用OSGI模块化标准构建Java EE项目,其中比较繁琐的一个方面就是Bundle资源的获取,因为很多开源框架官方都没有发布Bundle版本的Jar文件,这也是使用OSGI开发企业应用首先要解决的问题。

本篇文章中,我们来总结一下Bundle资源的获取途径,包括如下几种方式:

一、从SpringSource Enterprise Bundle Repository下载

这种方式前面文章中已经提到过,SpringSource Enterprise Bundle Repository是目前最全面的Bundle资源网站,地址如下:

http://ebr.springsource.com/repository/app/bundle

首页面如下图:

我们可以根据首字母进行Bundle资源的分类查找,也可以在网站右侧搜索框进行模糊搜索,SpringSource Enterprise Bundle Repository网站提供了大量的工具包及开源框架的Bundle资源,日常开发中需要的绝大多数的Bundle都可以从该网站获取。

二、从Eclipse Orbit获取

Eclipse Orbit是Eclipse组织维护的Bundle资源库,它的Bundle资源并不没有SpringSource Enterprise Bundle Repository那么全面,根据Eclipse Orbit官网介绍,它是用于提供Eclipse项目中依赖的第三方库或者框架的Bundle资源,并且所有第三方库或框架使用的开源协议必须与Eclipse Public Licence(EPL协议)兼容。

Eclipse Orbit官网地址:http://www.eclipse.org/orbit/

三、使用BND工具將Jar包转换为Bundle

这种方式非常实用,因为一些比较冷门的框架或者工具包,可能没有办法直接从网上下载到,这时候我们可以使用BND工具將普通的Jar包转换为Bundle。

下面就来介绍BND工具的获取与使用步骤:

1、下载BND工具

BND工具实际上就是一个Jar包,读者可以通过搜索引擎搜索bnd-2.1.0.jar,下载该文件。

笔者在CSDN网站上传了一份,下载地址如下:

http://download.csdn.net/detail/rongbo_j/9738274

获取到该文件后,可以通过如下命令运行工具查看帮助信息:

java -jar bnd-2.1.0.jar

帮助信息如下:

Available commands:

  baseline          - Compare a newer bundle to a baselined bundle and provide
versioning advice
bnd - The swiss army tool for OSGi
build - Build a project. This will create the jars defined in the
bnd.bnd and sub-builders.
buildx - Build project, is deprecated but here for backward
compatibility
bump - Bumps the version of a project
clean - Clean a project
convert - Converter to different formats
create - Create jar, used to support backward compatible java jar
commands
debug - Show a lot of info about the project you're in
deliverables - Show all deliverables from this workspace. with their
current version and path.
diff - Diff jars
digest - Digests a number of files
do - Execute a file based on its extension. Supported
extensions are: bnd (build), bndrun (run), and jar (print)
eclipse - Show info about the current directory's eclipse project
extract - Extract files from a JAR file, equivalent jar command
x[vf] (syntax supported)
grep - Grep the manifest of bundles/jar files.
info - Show key project variables
macro - Show macro value
maven - Maven bundle command
package - Package a bnd or bndrun file into a single jar that
executes with java -jar <>.jar
print - Printout the JAR
project - Execute a Project action, or if no parms given, show
information about the project
release - Release this project
repo - Manage the repositories
run - Run a project in the OSGi launcher
runtests - Run OSGi tests and create report
schema - Highly specialized function to create an overview of
package deltas in ees
select - Helps finding information in a set of JARs by filtering on
manifest data and printing out selected information.
settings - Set bnd/jpm global variables
source - Merge a binary jar with its sources. It is possible to
specify source path
syntax - Access the internal bnd database of keywords and options
test - Test a project according to an OSGi test
type - List files int a JAR file, equivalent jar command t[vf]
(syntax supported)
verify - Verify jars
version - Show version information about bnd
view - View a resource from a JAR file.
wrap - Wrap a jar
xref - Show a cross references for all classes in a set of jars.

可以看到,该工具可选的参数很多,但是我们实际用到的参数并不多,接下来介绍工具的使用。

2、將Jar包转换为Bundle

接着我们就使用BND工具將普通Jar包转换为Bundle,以dom4j-1.6.1.jar为例,將dom4j-1.6.1.jar和bnd-2.1.0.jar放在同一个目录中。

然后在该目录下新建一个org.csdn.dom4j-1.6.1.bnd文件,内容如下:

#org.csdn.dom4j-1.6.1.bnd
-classpath:dom4j-1.6.1.jar version:1.6.1
Bundle-SymbolicName:org.csdn.dom4j
Bundle-Version:${version}
Export-Package:*;version=${version}

然后执行如下命令:

java -jar bnd-2.1.0.jar org.csdn.dom4j-1.6.1.bnd

命令执行完毕后,就会在相同目录下生成一个org.csdn.dom4j-1.6.1.jar文件,如下图:



该文件已经是一个完整的Bundle了,我们解压org.csdn.dom4j-1.6.1.jar文件,然后查看META-INF/MANIFEST.MF文件,内容如下:

Manifest-Version: 1.0
Bnd-LastModified: 1484488037644
Bundle-ManifestVersion: 2
Bundle-Name: org.csdn.dom4j
Bundle-SymbolicName: org.csdn.dom4j
Bundle-Version: 1.6.1
Created-By: 1.8.0_11 (Oracle Corporation)
Export-Package: org.dom4j;version="1.6.1";uses:="org.dom4j.rule,org.dom4
j.tree,org.jaxen,org.xml.sax",org.dom4j.bean;version="1.6.1";uses:="org
.dom4j,org.dom4j.tree,org.xml.sax",org.dom4j.datatype;version="1.6.1";u
ses:="com.sun.msv.datatype,com.sun.msv.datatype.xsd,org.dom4j,org.dom4j
.tree,org.relaxng.datatype",org.dom4j.dom;version="1.6.1";uses:="org.do
m4j,org.dom4j.tree,org.w3c.dom",org.dom4j.dtd;version="1.6.1",org.dom4j
.io;version="1.6.1";uses:="javax.xml.namespace,javax.xml.stream,javax.x
ml.stream.events,javax.xml.stream.util,javax.xml.transform.sax,org.dom4
j,org.dom4j.tree,org.gjt.xpp,org.w3c.dom,org.xml.sax,org.xml.sax.ext,or
g.xml.sax.helpers,org.xmlpull.v1",org.dom4j.jaxb;version="1.6.1";uses:=
"javax.xml.bind,org.dom4j,org.dom4j.io,org.xml.sax",org.dom4j.rule;vers
ion="1.6.1";uses:="org.dom4j,org.jaxen",org.dom4j.rule.pattern;version=
"1.6.1";uses:="org.dom4j,org.dom4j.rule",org.dom4j.swing;version="1.6.1
";uses:="javax.swing.table,javax.swing.tree,org.dom4j,org.jaxen",org.do
m4j.tree;version="1.6.1";uses:="org.dom4j,org.dom4j.rule,org.xml.sax",o
rg.dom4j.util;version="1.6.1";uses:="org.dom4j,org.dom4j.rule,org.dom4j
.tree,org.jaxen,org.xml.sax",org.dom4j.xpath;version="1.6.1";uses:="org
.dom4j,org.dom4j.rule,org.jaxen,org.jaxen.pattern",org.dom4j.xpp;versio
n="1.6.1";uses:="org.dom4j,org.gjt.xpp"
Import-Package: com.sun.msv.datatype,com.sun.msv.datatype.xsd,javax.swin
g.table,javax.swing.tree,javax.xml.bind,javax.xml.namespace,javax.xml.p
arsers,javax.xml.stream,javax.xml.stream.events,javax.xml.stream.util,j
avax.xml.transform,javax.xml.transform.sax,javax.xml.transform.stream,o
rg.gjt.xpp,org.jaxen,org.jaxen.dom4j,org.jaxen.pattern,org.jaxen.saxpat
h,org.relaxng.datatype,org.w3c.dom,org.xml.sax,org.xml.sax.ext,org.xml.
sax.helpers,org.xmlpull.v1
Tool: Bnd-2.1.0.20130426-122245

实际上,普通Jar包和Bundle本质的区别也就在于MANIFEST.MF文件内容,BND工具的工作原理也只是生成MANIFEST.MF文件,指定导入导出的包而已。

除此之外,我们还可以在Bundle中直接使用Jar包,如果一个工具库只需要在其中一个Bundle中使用,只需要使用Bundle-ClassPath元数据头指定Jar包的路径即可,并不一定非要把Jar包转成Bundle去使用,本文内容就介绍这么多,后面文章会介绍如何使用OSGI开发WEB应用,以及如何整合MVC框架。

转载请注明本文地址:http://blog.csdn.net/Rongbo_J/article/details/54179073

OSGI企业应用开发(十一)Bundle资源获取途径的更多相关文章

  1. OSGI企业应用开发(十三)OSGI Web应用开发(二)

    上篇文章介绍了OSGI Web应用的两种开发模式,并把Jetty应用服务器以Bundle的形式整合到Equinox容器中,已这种模式开发Web应用,所有的应用程序资源,例如Servlet.JSP.HT ...

  2. OSGI企业应用开发(十二)OSGI Web应用开发(一)

    前面文章中介绍了如何在OSGI应用中整合Spring和Mybatis框架,本篇文章开始介绍如何使用OSGI技术开发Web应用.对于传统的Java EE应用,应用中涉及到的Web元素无非就是Servle ...

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

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

  4. OSGI企业应用开发(三)Eclipse中搭建Equinox运行环境

    上篇文章介绍了如何在Eclipse中搭建Felix的运行环境,我们需要將Bundle发布到Felix框架的bundle目录下,Felix框架启动时才会自动加载这些Bundle,否则需要在Felix框架 ...

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

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

  6. OSGI企业应用开发(一)OSGI简介

    一.OSGI简介 OSGI全称为Open Service Gateway Initiative(开放服务网关规范),有两个层面的含义,一方面它指OSGi Alliance组织:另一方面指该组织制定的一 ...

  7. OSGI企业应用开发(十四)整合Spring、Mybatis、Spring MVC

    作为一个企业级的Web应用,MVC框架是必不可少的.Spring MVC目前使用也比较广泛,本文就来介绍一下如何在OSGI应用中实现Spring.Mybatis.Spring MVC框架的整合,其中S ...

  8. OSGI企业应用开发(八)整合Spring和Mybatis框架(一)

    到目前为止,我们已经学习了如何使用Blueprint將Spring框架整合到OSGI应用中,并学习了Blueprint&Gemini Blueprint的一些使用细节.本篇文章开始,我们將My ...

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

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

随机推荐

  1. 05-01 Java 方法

    方法 方法定义.格式: /* 方法:完成特定功能的代码块. 注意:在很多语言里面有函数的定义,而在Java中函数被称为方法. 方法格式: 修饰符 返回值类型 方法名(参数类型 参数名1,参数类型 参数 ...

  2. odoo开发笔记 -- 视图继承扩展

    参考: http://www.jeffzhang.cn/Odoo-Notes-2/ http://blog.csdn.net/zhangfeng1133/article/details/4693517 ...

  3. 剑指offer十九之顺时针打印矩阵

    一.题目 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2, ...

  4. 剑指offer十七姊妹篇之二叉树的创建、遍历、判断子二叉树

    1.二叉树节点类 public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public Tr ...

  5. git中多账号切换问题的解决方案(转)

    作者:知乎用户链接:https://www.zhihu.com/question/23028445/answer/416231632来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请 ...

  6. Storm中的定时任务

    1.全局定时器 import java.util.Map; import backtype.storm.Config; import backtype.storm.Constants; import ...

  7. webgl介绍

    一.webgl与three.js 我们知道canvas.svg等是2D绘图的,那么如果想要使用js进行3D绘图,可以吗? 答案是肯定的!实际上主流的3D开发使用的是c++,但是随着技术的发展,Java ...

  8. JVM 运行时数据区详解

    一.运行时数据区 Java虚拟机在执行Java程序的过程中会把它所管理的内存划分为若干个不同数据区域. 1.有一些是随虚拟机的启动而创建,随虚拟机的退出而销毁,所有的线程共享这些数据区. 2.第二种则 ...

  9. CFileDialog类的详情

    CFileDialog类封装了Windows常用的文件对话框. 常用的文件对话框提供了一种简单的与Windows标准相一致的文件打开和文件存盘对话框功能. void CnotepadDlg::OnOp ...

  10. 人人,金山西山居,腾讯互娱,微信,网易游戏offer及面经

    转自:http://www.itmian4.com/forum.php?mod=viewthread&tid=3985 首先感谢师兄在两年前发的贴([天道酬勤] 腾讯.百度.网易游戏.华为Of ...