Bundlor 入门

工程 | Ben Hale | 2009年3月20日 | ...

正如 Rob 的帖子所指出的,在过去的几个月里,我们对人们希望如何管理自己的 OSGi 应用程序有了一些了解。

我们发现,一些开发人员希望自己管理 bundle manifest,但需要一些帮助来自动化诸如为一系列导入指定包版本之类的事宜。其他开发人员则希望根据其项目的实际内容和构建文件中指定的依赖项来生成 manifest。此外,这两类开发人员都需要与现有库协同工作,这些库可能缺少启用它们在 OSGi 服务平台上使用的必要 OSGi 元数据。

Bundlor 为所有这些情况提供了一个解决方案,并且是我们一段时间以来用于管理发布到 SpringSource Enterprise Bundle Repository 的 bundles 的工具。Bundlor 可以在 JAR 创建后自动检测依赖项并为 JAR 创建 OSGi manifest 指令。它以 JAR 和一个由标准 OSGi manifest 头组成的超集作为输入。然后,它分析 JAR 中包含的源代码和支持文件,将模板应用于结果,并生成 manifest。

模板机制

Bundlor 模板机制使用标准的 Java manifest 格式,并由标准的 OSGi manifest 头组成的超集构成。下表列出了所有 Bundlor 特定的 manifest 模板头及其用法。
Header 描述
Excluded-Exports 必须不添加到 manifest 的逗号分隔的包列表Export-Packageheader。
Excluded-Imports 默认情况下,Bundlor 将为它确定被 jar 中的代码或特殊文件引用的每个包添加导入。此 header 允许指定一个逗号分隔的包列表,这些包将不会生成导入。
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 可用于列出 Bundlor 应忽略的原始 manifest 中的头。
Import-Template 此 header 用于增强 Bundlor 通过字节码和特殊文件分析生成的包导入。通常这是为了指定导入的版本,有时也用于将其标记为可选。header 的值是逗号分隔的包名称和属性列表。
在包名称末尾的通配符 '*' 支持匹配多个包。例如:Import-Template: com.foo;version=[1.0,2.0);resolution:=optional,com.bar.*;version="[1.5,1.6)"将导致为com.foo包生成的任何导入都将版本号设置为从 1.0(含)到 2.0(不含),并被视为可选;对于任何导入com.bar或其子包,版本号将设置为从 1.5(含)到 1.6(不含)。

以下是来自 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

检测标准

给定一个 JAR 文件,Bundlor 可以从多种来源检测候选导入。有关可检测内容的完整列表和示例,请参阅用户指南

Bundlor 扫描以下类型

  • Java 类
    • 声明的类型超类类型
    • 声明的类型实现接口类型
    • 声明的类型注解类型
    • 声明的字段类型
    • 声明的字段值类型
    • 声明的方法参数类型
    • 声明的方法返回类型
    • 声明的方法异常类型
    • 声明的方法注解类型
    • 字段所有者类型引用
    • 字段类型引用
    • 声明的局部变量类型
    • 方法声明类型引用
    • 方法返回类型引用
    • 方法参数类型引用
    • 数组类型分配
    • 声明的参数注解类型
    • 捕获的异常类型
    • 实例化的类型
    • 强制转换目标类型
    • instanceof 类型
    • 声明的常量类型
  • Spring context 配置文件
    • 指定的类名,未来将通过 Spring schema 的知识进行改进
  • JPA
    • provider还是类中看出的那样:来自persistence.xml文件的标签
  • Hibernate 映射文件
    • //class/@name
    • //id/@type
    • //generator/@class
    • //composite-id/@class
    • //discriminator/@type
    • //property/@type
    • //many-to-one/@class
    • //one-to-one/@class
    • //one-to-many/@class
    • //many-to-many/@class
    • //version/@type
    • //component/@class
    • //dynamic-component/@class
    • //subclass/@name
    • //joined-subclass/@name
    • //union-subclass/@name
    • //import/@class
  • 属性文件
    • 作为属性值定义的类

获取 Bundlor

Bundlor 可从Bundlor 页面下载 zip 文件。它也可以在 SpringSource Enterprise Bundle Repository 中作为 Ivy 和 Maven 构件访问。此外,还有一本详细的用户指南

使用 Bundlor

Bundlor 有四种不同的形式
  • Apache Maven 的插件
  • Apache ANT 的任务
  • 命令行应用程序
  • Eclipse 插件(更多信息将在单独的博客中介绍)
以下说明仅描述了 Bundlor 在每种环境中的简单用法。有关 Bundlor 的语法,请参阅用户指南

Maven

Maven 插件允许从 Maven 项目内部运行 Bundlor。通过此任务,可以将 JAR 转换为 bundle,或者将 manifest 写入文件系统。

将 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

ANT 任务允许从任何基于 ANT 的构建系统内部运行 Bundlor。通过此任务,可以将 JAR 转换为 bundle。

要从 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>

最后,使用bundlortask。

<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"/>

命令行

命令行界面是一种快速使用 manifest 模板的方法,可以显示 bundle 的 Bundlor 生成的 OSGi manifest,或实际转换 bundle。

要从命令行运行 Bundlor,请切换到目录$BUNDLOR_HOME/bin目录并运行任一bundlor.shbundlor.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
%

未来的 Bundlor

Bundlor 是一个新产品,有很大的发展空间。Bundlor 现在使用 Scrum 进行规划,我们希望借此能让社区更清楚地了解开发过程。当前冲刺和发布积压工作可在Bundlor JIRA上找到。Bundlor 的未来发展方向由我们的用户决定,因此请花时间为现有问题投票,并为积压工作中尚未涵盖的事项创建新问题。

获取 Spring 新闻通讯

通过 Spring 新闻通讯保持联系

订阅

领先一步

VMware 提供培训和认证,助您加速进步。

了解更多

获得支持

Tanzu Spring 提供 OpenJDK™、Spring 和 Apache Tomcat® 的支持和二进制文件,只需一份简单的订阅。

了解更多

即将举行的活动

查看 Spring 社区所有即将举行的活动。

查看所有