OSGI企业应用开发(十一)Bundle资源获取途径

版权声明:本文为博主原创文章,转载请注明原文地址。 https://blog.csdn.net/Rongbo_J/article/details/54179073

使用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

猜你喜欢

转载自blog.csdn.net/Rongbo_J/article/details/54179073