使用 OSGi 配置文件与 Bundlor

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

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

随着 1.0.0.M4 的发布,Bundlor 获得了将 OSGi 配置文件作为输入,并自动为启动委托的包和系统捆绑包添加模板条目的能力。此输入以属性文件的形式获取,可与命令行、ANT 和 Maven 配合使用。

OSGi 配置文件

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 条目。相反,我将再次运行 Bundlor,并使用 dm Server OSGi 配置文件。此配置文件从系统捆绑包导出这些包中的每一个。这次运行没有警告,并以 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 社区所有即将举行的活动。

查看所有