Bundlor 入門

工程 | Ben Hale | 2009 年 3 月 20 日 | ...

如同 Rob 的文章 指出,過去幾個月我們學到了很多人們想要如何管理自己的 OSGi 應用程式。

我們發現有些開發人員想要管理自己的 bundle 宣告檔案,但需要一些協助來自動化細節,例如指定一系列匯入的套件版本。 其他開發人員想要根據專案內容和建置檔案中指定的相依性來產生宣告檔案。 此外,這兩種開發人員都需要使用現有的函式庫,而這些函式庫沒有必要的 OSGi metadata,使其無法在 OSGi 服務平台上使用。

Bundlor 為所有這些情況提供了解決方案,而且是我們內部使用了一段時間的工具,用於管理發佈到 SpringSource Enterprise Bundle Repository 的 bundle。 Bundlor 會自動偵測相依性,並在建立 JAR 之後建立 JAR 的 OSGi 宣告指令。 它會將 JAR 和範本(包含標準 OSGi 宣告標頭的超集合)作為輸入。 然後,它會分析 JAR 中包含的原始程式碼和支援檔案,將範本套用到結果,並產生宣告。

範本機制

Bundlor 範本機制使用標準 Java 宣告格式,並包含標準 OSGi 宣告標頭的超集合。 下表列出了所有 Bundlor 特定的宣告範本標頭及其用法。
標頭 說明
Excluded-Exports 以逗號分隔的套件清單,這些套件不得新增至宣告檔案的Export-Package標頭。
Excluded-Imports 預設情況下,Bundlor 會為它判斷為程式碼或 jar 中的特殊檔案所參考的每個套件新增匯入。 此標頭允許指定以逗號分隔的套件清單,這些套件將不會產生匯入。
Export-Template 預設情況下,Bundlor 會以指定的版本來版本化所有匯出的套件Bundle-Version。 此標頭允許以不同的版本匯出個別的匯出套件。 例如:Export-Template com.foo.*;version="1.5"會導致任何Export-Package的項目com.foo或其子套件的版本為1.5.
Ignored-Existing-Headers 對於產生宣告的 JAR 已經包含符合 OSGi 的宣告的情況,可以使用此標頭來列出 Bundlor 應該忽略的原始宣告中的標頭。
Import-Template 此標頭用於擴充 Bundlor 透過位元組碼和特殊檔案分析產生的套件匯入。 通常,這是為了版本化匯入,並且在某些情況下,將它們標記為可選。 標頭的值是套件名稱和屬性的逗號分隔清單。
支援在套件名稱結尾使用萬用字元 '*' 來比對多個套件。 例如:Import-Template: com.foo;version=[1.0,2.0);resolution:=optional,com.bar.*;version="[1.5,1.6)"會導致為com.foo套件產生的任何匯入的版本為包含 1.0 到不包含 2.0,並且被視為可選,以及任何匯入com.bar或其子套件的版本為包含 1.5 到不包含 1.6。

以下是 Spring Binding bundle 中的 Bundlor 宣告範本範例,顯示了萬用字元和明確的Import-Package語句的使用。

Bundle-ManifestVersion: 2
Bundle-SymbolicName: org.springframework.binding
Bundle-Name: Spring Binding
Bundle-Vendor: SpringSource
Import-Package:
 ognl;version="[2.6.9, 3.0.0)";resolution:=optional,
 org.jboss.el;version="[2.0.0, 3.0.0)";resolution:=optional
Import-Template:
 org.springframework.*;version="[2.5.4.A, 3.0.0)",
 org.apache.commons.logging;version="[1.1.1, 2.0.0)",
 javax.el;version="[2.1.0, 3.0.0)";resolution:=optional

偵測標準

給定 JAR 檔案,Bundlor 可以從各種來源偵測候選匯入。 有關可以偵測到的完整清單和範例,請參閱 使用者指南

Bundlor 會掃描下列類型

  • Java 類別
    • 宣告的類型超類別類型
    • 宣告的類型實作的介面類型
    • 宣告的類型註解類型
    • 宣告的欄位類型
    • 宣告的欄位值類型
    • 宣告的方法引數類型
    • 宣告的方法傳回類型
    • 宣告的方法例外類型
    • 宣告的方法註解類型
    • 參考欄位擁有者類型
    • 參考欄位類型
    • 宣告的區域變數類型
    • 參考方法的宣告類型
    • 參考方法的傳回類型
    • 參考方法的引數類型
    • 陣列類型的配置
    • 宣告的參數註解類型
    • 捕獲的例外類型
    • 實例化的類型
    • 轉換目標類型
    • Instanceof 類型
    • 宣告的常數類型
  • Spring 內容設定檔案
    • 指定的類別名稱,並且未來將透過 Spring 綱要的知識來改進
  • JPA
    • 提供者類別來自persistence.xml檔案
  • Hibernate 對應檔案
    • //class/@name
    • //id/@type
    • //generator/@class
    • //composite-id/@class
    • //discriminator/@type
    • //property/@type
    • //many-to-one/@class
    • //one-to-one/@class
    • //one-to-many/@class
    • //many-to-many/@class
    • //version/@type
    • //component/@class
    • //dynamic-component/@class
    • //subclass/@name
    • //joined-subclass/@name
    • //union-subclass/@name
    • //import/@class
  • Properties 檔案
    • 定義為屬性值的類別

取得 Bundlor

Bundlor 可以從 Bundlor 頁面 以 zip 檔案下載。 也可以在 SpringSource Enterprise Bundle Repository 中以 Ivy 和 Maven 成品的形式存取。 此外,還有詳細的 使用者指南 可用。

使用 Bundlor

Bundlor 有四種不同的形式
  • Apache Maven 的外掛程式
  • Apache ANT 的任務
  • 命令列應用程式
  • Eclipse 外掛程式(更多資訊請參閱另一篇部落格)
以下說明僅描述在每個環境中 Bundlor 的簡單用法。 請參閱 使用者指南 以取得 Bundlor 語法的完整資訊。

Maven

Maven 外掛程式允許從 Maven 專案內部執行 Bundlor。 使用此任務,可以將 JAR 轉換為 bundle,或將宣告寫入檔案系統。

將 SpringSource Enterprise Bundle Repository 新增至pom.xml檔案。

<pluginRepositories>
  <pluginRepository>
    <id>com.springsource.repository.bundles.milestone</id>
    <name>SpringSource Enterprise Bundle Repository</name>
    <url>http://repository.springsource.com/maven/bundles/milestone</url>
  </pluginRepository>
...
</pluginRepositories>

新增bundlor外掛程式至pom.xml檔案

<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>
        </execution>
      </executions>
    </plugin>
  ...
  </plugins>
...
</build>

最後,使用 package 命令建置 bundle。

mvn install package

ANT

ANT 任務允許從任何基於 ANT 的建置系統內部執行 Bundlor。 使用此任務,可以將 JAR 轉換為 bundle。

若要從 ANT 內部執行 Bundlor,請先定義一個bundlor命名空間。

<project name="bundlor-sample-ant"
    xmlns:bundlor="antlib:com.springsource.bundlor.ant">

然後將 bundlor 任務匯入到建置中。

<target name="bundlor.init">
  <taskdef resource="com/springsource/bundlor/ant/antlib.xml"
      uri="antlib:com.springsource.bundlor.ant">
    <classpath id="bundlor.classpath">
      <fileset dir="${bundlor.home}/dist"/>
      <fileset dir="${bundlor.home}/lib"/>
    </classpath>
  </taskdef>
</target>

最後,使用bundlor任務。

<bundlor:bundlor
    bundlePath="${basedir}/org.springframework.integration.jar"
    outputPath="${basedir}/target/org.springframework.integration.jar"
    bundleVersion="1.0.2.BUILD-${timestamp}"
    manifestTemplatePath="${basedir}/template.mf"/>

命令列

命令列介面是使用宣告範本來顯示 bundle 的 Bundlor 產生 OSGi 宣告或實際轉換 bundle 的快速方法。

若要從命令列執行 Bundlor,請將目錄變更為$BUNDLOR_HOME/bin目錄,然後執行bundlor.shbundlor.bat.

% ./bundlor.sh transform \
--bundle ./org.springframework.integration.jar \
--manifest ./template.mf \
--outputfile ./target/org.springframework.integration.jar

Transformed bundle written to ./target/org.springframework.integration.jar
%

未來的 Bundlor

Bundlor 是一個新產品,還有很大的成長空間。 Bundlor 現在使用 Scrum 進行規劃,我們希望讓社群更了解開發流程。 目前的 sprint 和發佈待辦事項可在 Bundlor JIRA 上取得。 Bundlor 的未來路徑由我們的使用者決定,因此請花時間投票支持現有的問題,並為目前待辦事項未涵蓋的事項建立新問題。

取得 Spring 電子報

透過 Spring 電子報保持聯繫

訂閱

領先一步

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

瞭解更多

取得支援

Tanzu Spring 在一個簡單的訂閱中提供 OpenJDK™、Spring 和 Apache Tomcat® 的支援和二進位檔。

瞭解更多

即將舉行的活動

查看 Spring 社群中所有即將舉行的活動。

檢視所有