保持领先
VMware 提供培训和认证,助您加速进步。
了解更多正如 Rob 的文章 所指出的,过去几个月里,我们对人们如何管理自己的 OSGi 应用程序有了不少了解。
我们发现,一些开发者希望自己管理 bundle manifests,但需要一些帮助来自动化细节,例如跨各种导入指定包版本。另一些开发者则希望根据项目内容和构建文件中指定的依赖关系生成 manifests。此外,这两类开发者都需要使用没有必要 OSGi 元数据(使其能够在 OSGi 服务平台中使用)的现有库。
Bundlor 为所有这些情况提供了解决方案,并且是我们内部长期以来用于管理发布到 SpringSource Enterprise Bundle Repository 的 bundles 的工具。Bundlor 自动化检测依赖关系并在 JAR 创建后生成 OSGi manifest 指令。它接收一个 JAR 和一个包含标准 OSGi manifest header 超集的模板作为输入。然后它分析 JAR 中包含的源代码和支持文件,将模板应用于结果,并生成一个 manifest。
| Header | 描述 | 
|---|---|
| Excluded-Exports | 一个逗号分隔的包列表,不得添加到 manifest 的Export-Packageheader 中。 | 
| Excluded-Imports | 默认情况下,Bundlor 会为它确定由 jar 中的代码或特殊文件引用的每个包添加导入。此 header 允许指定一个逗号分隔的包列表,Bundlor 不会为其生成导入。 | 
| Export-Template | 默认情况下,Bundlor 将所有导出的包版本设为指定的Bundle-Version。此 header 允许单独导出的包以不同的版本导出。例如:Export-Template com.foo.*;version="1.5"这将导致任何Export-Package的条目,对于com.foo或其子包,版本为1.5. | 
| Ignored-Existing-Headers | 对于要生成 manifest 的 JAR 已包含 OSGi 兼容 manifest 的情况,此 header 可用于列出原始 manifest 中 Bundlor 应忽略的 header。 | 
| Import-Template | 此 header 用于增强 Bundlor 通过字节码和特殊文件分析生成的包导入。通常,这用于指定导入版本,并在某些情况下将其标记为可选。header 的值采用逗号分隔的包名和属性列表形式。 | 
以下是一个来自 Spring Binding bundle 的 Bundlor manifest 模板示例,展示了通配符和显式Import-Package语句的用法。
Bundle-ManifestVersion: 2
Bundle-SymbolicName: org.springframework.binding
Bundle-Name: Spring Binding
Bundle-Vendor: SpringSource
Import-Package:
 ognl;version="[2.6.9, 3.0.0)";resolution:=optional,
 org.jboss.el;version="[2.0.0, 3.0.0)";resolution:=optional
Import-Template:
 org.springframework.*;version="[2.5.4.A, 3.0.0)",
 org.apache.commons.logging;version="[1.1.1, 2.0.0)",
 javax.el;version="[2.1.0, 3.0.0)";resolution:=optional
Bundlor 扫描以下类型:
将 SpringSource Enterprise Bundle Repository 添加到pom.xml文件。
<pluginRepositories>
  <pluginRepository>
    <id>com.springsource.repository.bundles.milestone</id>
    <name>SpringSource Enterprise Bundle Repository</name>
    <url>http://repository.springsource.com/maven/bundles/milestone</url>
  </pluginRepository>
...
</pluginRepositories>
添加bundlor插件到pom.xml文件
<build>
  <plugins>
    <plugin>
      <groupId>com.springsource.bundlor</groupId>
      <artifactId>com.springsource.bundlor.maven</artifactId>
      <version>1.0.0.M2</version>
      <executions>
        <execution>
          <id>bundlor</id>
          <goals>
            <goal>transform</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  ...
  </plugins>
...
</build>
最后,使用 package 命令构建 bundle。
mvn install package
要在 ANT 内部运行 Bundlor,首先需要定义一个bundlornamespace。
<project name="bundlor-sample-ant"
    xmlns:bundlor="antlib:com.springsource.bundlor.ant">
然后将 bundlor 任务导入到构建中。
<target name="bundlor.init">
  <taskdef resource="com/springsource/bundlor/ant/antlib.xml"
      uri="antlib:com.springsource.bundlor.ant">
    <classpath id="bundlor.classpath">
      <fileset dir="${bundlor.home}/dist"/>
      <fileset dir="${bundlor.home}/lib"/>
    </classpath>
  </taskdef>
</target>
最后,使用bundlor任务。
<bundlor:bundlor
    bundlePath="${basedir}/org.springframework.integration.jar"
    outputPath="${basedir}/target/org.springframework.integration.jar"
    bundleVersion="1.0.2.BUILD-${timestamp}"
    manifestTemplatePath="${basedir}/template.mf"/>
要在命令行运行 Bundlor,请将目录更改到$BUNDLOR_HOME/bin目录,并运行bundlor.sh或bundlor.bat.
% ./bundlor.sh transform \ --bundle ./org.springframework.integration.jar \ --manifest ./template.mf \ --outputfile ./target/org.springframework.integration.jar Transformed bundle written to ./target/org.springframework.integration.jar %