使用 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 社群所有即將舉行的活動。

檢視所有