搭配 Bundlor 使用 OSGi 設定檔

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

當管理和轉換大量包含在 SpringSource Enterprise Bundle Repository 中的 bundles 時,要記住哪些套件已啟動委派(boot delegated)、從系統 bundle 匯出,或從系統中的其他 bundles 匯出,會變得非常困難。此資訊很重要,因為您可能不想匯入已啟動委派的套件,您可能想要以 "0" 的版本匯入系統 bundle 套件,並且您想要為所有其他套件定義自訂匯入。記住哪些套件屬於這些類別最終容易出錯,並且為它們定義範本項目可能很耗時。

隨著 1.0.0.M4 的發布,Bundlor 獲得了將 OSGi 設定檔作為輸入,並自動為啟動委派的套件和系統 bundles 新增範本項目的能力。此輸入採用屬性檔案的形式,並且可以與命令列、ANT 和 Maven 一起使用。

OSGi 設定檔

OSGi 設定檔定義了從系統 bundle 匯出的套件,以及委派給啟動類別載入器的套件。此設定檔不是特定的檔案,而是 OSGi 執行階段所熟知的兩個屬性。屬性 org.osgi.framework.system.packages 定義了從系統 bundle 匯出的套件,而 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。此設定檔從系統 bundle 匯出每個這些套件。此執行在沒有警告的情況下返回,並以 version="0" 匯入每個這些套件。此 bundle 不依賴任何啟動委派的套件,因此我們的 manifest 中沒有忽略的匯入。

用法

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 設定檔作為輸入的能力極大地簡化了 manifest 範本的內容。如果您能想到更多可以改進的地方,請在 Bundlor JIRA 上開啟一個 issue 或投票給現有的 issue。

獲取 Spring 電子報

通過 Spring 電子報保持聯繫

訂閱

領先一步

VMware 提供培訓和認證,以加速您的進展。

了解更多

獲得支持

Tanzu Spring 在一個簡單的訂閱中提供對 OpenJDK™、Spring 和 Apache Tomcat® 的支持和二進制文件。

了解更多

即將到來的活動

查看 Spring 社群中所有即將到來的活動。

查看全部