Bundlor 入門

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

正如 Rob 的帖子所指出的,在過去的幾個月裡,我們對人們希望如何管理自己的 OSGi 應用程式有了一些瞭解。

我們發現,一些開發人員希望自己管理 bundle manifest,但需要一些幫助來自動化諸如為一系列匯入指定包版本之類的事宜。其他開發人員則希望根據其專案的實際內容和構建檔案中指定的依賴項來生成 manifest。此外,這兩類開發人員都需要與現有庫協同工作,這些庫可能缺少啟用它們在 OSGi 服務平臺上使用的必要 OSGi 元資料。

Bundlor 為所有這些情況提供了一個解決方案,並且是我們一段時間以來用於管理釋出到 SpringSource Enterprise Bundle Repository 的 bundles 的工具。Bundlor 可以在 JAR 建立後自動檢測依賴項併為 JAR 建立 OSGi manifest 指令。它以 JAR 和一個由標準 OSGi manifest 頭組成的超集作為輸入。然後,它分析 JAR 中包含的原始碼和支援檔案,將模板應用於結果,並生成 manifest。

模板機制

Bundlor 模板機制使用標準的 Java manifest 格式,並由標準的 OSGi manifest 頭組成的超集構成。下表列出了所有 Bundlor 特定的 manifest 模板頭及其用法。
Header 描述
Excluded-Exports 必須不新增到 manifest 的逗號分隔的包列表Export-Packageheader。
Excluded-Imports 預設情況下,Bundlor 將為它確定被 jar 中的程式碼或特殊檔案引用的每個包新增匯入。此 header 允許指定一個逗號分隔的包列表,這些包將不會生成匯入。
Export-Template 預設情況下,Bundlor 會為所有匯出的包指定版本Bundle-Version。此 header 允許為單個匯出的包指定不同的版本。例如:Export-Template com.foo.*;version="1.5"將導致任何Export-Package條目,用於com.foo或其子包,版本將為1.5.
Ignored-Existing-Headers 對於正在生成 manifest 的 JAR 已經包含 OSGi 相容 manifest 的情況,此 header 可用於列出 Bundlor 應忽略的原始 manifest 中的頭。
Import-Template 此 header 用於增強 Bundlor 透過位元組碼和特殊檔案分析生成的包匯入。通常這是為了指定匯入的版本,有時也用於將其標記為可選。header 的值是逗號分隔的包名稱和屬性列表。
在包名稱末尾的萬用字元 '*' 支援匹配多個包。例如: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 manifest 模板示例,展示了萬用字元和顯式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 context 配置檔案
    • 指定的類名,未來將透過 Spring schema 的知識進行改進
  • JPA
    • provider還是類中看出的那樣:來自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
  • 屬性檔案
    • 作為屬性值定義的類

獲取 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,或者將 manifest 寫入檔案系統。

將 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,首先需要定義一個bundlornamespace。

<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>

最後,使用bundlortask。

<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"/>

命令列

命令列介面是一種快速使用 manifest 模板的方法,可以顯示 bundle 的 Bundlor 生成的 OSGi manifest,或實際轉換 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 進行規劃,我們希望藉此能讓社群更清楚地瞭解開發過程。當前衝刺和釋出積壓工作可在Bundlor JIRA上找到。Bundlor 的未來發展方向由我們的使用者決定,因此請花時間為現有問題投票,併為積壓工作中尚未涵蓋的事項建立新問題。

獲取 Spring 新聞通訊

透過 Spring 新聞通訊保持聯絡

訂閱

領先一步

VMware 提供培訓和認證,助您加速進步。

瞭解更多

獲得支援

Tanzu Spring 提供 OpenJDK™、Spring 和 Apache Tomcat® 的支援和二進位制檔案,只需一份簡單的訂閱。

瞭解更多

即將舉行的活動

檢視 Spring 社群所有即將舉行的活動。

檢視所有