使用 OSGi Profile 与 Bundlor

工程 | Ben Hale | 2009 年 5 月 18 日 | ...

当管理和转换像 SpringSource 企业捆绑仓库 中包含的那么多捆绑包时,记住哪些软件包被引导委托、从系统捆绑包导出或从系统中的其他捆绑包导出变得非常困难。 此信息很重要,因为您可能不想导入引导委托的软件包,您可能希望以“0”导入系统捆绑包软件包,并且您想为所有其他软件包定义自定义导入。记住哪些软件包属于每个类别最终会有些容易出错,并且为它们定义模板条目可能很耗时。

随着 1.0.0.M4 的发布,Bundlor 获得了将 OSGi 配置文件作为输入并自动为引导委托的软件包和系统捆绑包添加模板条目的能力。 此输入作为属性文件提供,可用于命令行、ANT 和 Maven。

OSGi Profile

OSGi 配置文件定义了从系统捆绑包导出的软件包和委托给引导类加载器的软件包。 此配置文件不是特定文件,而是 OSGi 运行时众所周知的两个属性。 属性 org.osgi.framework.system.packages 定义从系统捆绑包导出的软件包,org.osgi.framework.bootdelegation 定义引导委托的软件包。 有关这些属性值的语法的更多信息,请参见 OSGi 规范 §3.8.3 和 §3.8.5。

对于 Bundlor,这些属性定义在标准 .properties 文件中,例如以下文件。

org.osgi.framework.system.packages = \
 javax.accessibility,\
 javax.activation,\
 javax.activation;version="1.1.0",\
 javax.activity,\
 javax.annotation,\
...

org.osgi.framework.bootdelegation = \
 com_cenqua_clover,\
 com.cenqua.*,\
 com.yourkit.*,\
...

效果

作为此工作方式的一个示例,我采用了略微修改的 Apache Log4J 的 Bundlor 模板。 我使用的模板如下所示
Bundle-SymbolicName: com.springsource.org.apache.log4j
Bundle-Name: Apache Log4J
Bundle-Vendor: SpringSource
Bundle-ManifestVersion: 2
Import-Template: 
 com.sun.jdmk.*;version="[5.1.0, 5.1.0]";resolution:=optional,
 javax.jms.*;version="[1.1.0, 2.0.0)";resolution:=optional,
 javax.mail.*;version="[1.4.0, 2.0.0)";resolution:=optional

在没有配置文件的情况下针对 Bundlor 运行时,我收到以下警告

SB0001W: The import of package javax.management does not specify a version.
SB0001W: The import of package javax.naming does not specify a version.
SB0001W: The import of package javax.swing does not specify a version.
SB0001W: The import of package javax.swing.border does not specify a version.
SB0001W: The import of package javax.swing.event does not specify a version.
SB0001W: The import of package javax.swing.table does not specify a version.
SB0001W: The import of package javax.swing.text does not specify a version.
SB0001W: The import of package javax.swing.tree does not specify a version.
SB0001W: The import of package javax.xml.parsers does not specify a version.
SB0001W: The import of package org.w3c.dom does not specify a version.
SB0001W: The import of package org.xml.sax does not specify a version.
SB0001W: The import of package org.xml.sax.helpers does not specify a version.

为了满足这些警告,我可以添加多个 Import-Template 条目。 相反,我将使用 dm Server OSGi 配置文件 再次运行 Bundlor。 此配置文件从系统捆绑包导出每个这些软件包。 此运行返回时没有警告,并以 version="0" 导入每个这些软件包。 此捆绑包不依赖于任何引导委托的软件包,因此我们的清单中没有忽略导入。

用法

ANT

ANT 任务新增了一个可选的 osgiProfilePath 属性。
<bundlor:bundlor
    bundlePath="${basedir}/org.springframework.integration.jar"
    outputPath="${basedir}/target/org.springframework.integration.jar"
    bundleVersion="1.0.2.BUILD-${timestamp}"
    osgiProfilePath="java6-server.profile"
    manifestTemplatePath="${basedir}/template.mf"/>

Maven

Maven 插件新增了一个可选的 osgiProfilePath 配置。
<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>
          <configuration>
            <osgiProfilePath>java6-server.profile</osgiProfilePath>
          </configuration>
        </execution>
      </executions>
    </plugin>
  ...
  </plugins>
...
</build>

命令行

命令行界面为 manifesttransform 执行都新增了一个可选的 -p, --profile 参数。
bin/bundlor.sh transform
    -b com.springsource.org.apache.log4j.jar
    -m com.springsource.org.apache.log4j.mf
    -p java6-server.profile 

读取 OSGi 配置文件作为输入的能力大大简化了清单模板的内容。 如果您想到更多可以改进的地方,请在 Bundlor JIRA 上开设一个问题或投票支持现有问题。

获取 Spring 新闻资讯

订阅 Spring 新闻资讯,保持联系

订阅

更进一步

VMware 提供培训和认证来加速您的进步。

了解更多

获取支持

Tanzu Spring 在一个简单的订阅中提供 OpenJDK™、Spring 和 Apache Tomcat® 的支持和二进制文件。

了解更多

即将举行的活动

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

查看全部