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

檢視全部