Bundlor 入門

工程 | Ben Hale | March 20, 2009 | ...

正如 Rob 的文章 所指出的,過去幾個月裡,我們對人們如何管理自己的 OSGi 應用程式有了不少了解。

我們發現,一些開發者希望自己管理 bundle manifests,但需要一些幫助來自動化細節,例如跨各種匯入指定包版本。另一些開發者則希望根據專案內容和構建檔案中指定的依賴關係生成 manifests。此外,這兩類開發者都需要使用沒有必要 OSGi 元資料(使其能夠在 OSGi 服務平臺中使用)的現有庫。

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

模板機制

Bundlor 模板機制使用標準的 Java manifest 格式,幷包含標準 OSGi manifest header 的超集。下表列出了所有 Bundlor 特定的 manifest 模板 header 及其用法。
Header 描述
Excluded-Exports 一個逗號分隔的包列表,不得新增到 manifest 的Export-Packageheader 中。
Excluded-Imports 預設情況下,Bundlor 會為它確定由 jar 中的程式碼或特殊檔案引用的每個包新增匯入。此 header 允許指定一個逗號分隔的包列表,Bundlor 不會為其生成匯入。
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 可用於列出原始 manifest 中 Bundlor 應忽略的 header。
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)(包含 1.0,不包含 2.0),並被視為可選;而對於任何匯入com.bar或其子包,版本範圍為 [1.5, 1.6)(包含 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 上下文配置檔案
    • 指定的類名,未來將透過 Spring schema 的知識進行改進
  • JPA
    • providerclass標籤,來自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 檔案。它也可以作為 Ivy 和 Maven artifact 在 SpringSource Enterprise Bundle Repository 中獲取。此外,還提供了詳細的使用者指南

使用 Bundlor

Bundlor 有四種不同的形式:
  • Apache Maven 外掛
  • Apache ANT Task
  • 命令列應用程式
  • 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>

最後,使用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"/>

命令列

命令列介面是使用 manifest 模板快速顯示 Bundlor 生成的 bundle 的 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 進行規劃,我們希望這能讓社群更清晰地瞭解開發過程。當前的 sprint 和釋出 backlog 可以在Bundlor JIRA 上找到。Bundlor 未來的方向由我們的使用者決定,因此請花時間對現有問題進行投票,併為 backlog 中目前未涵蓋的內容建立新問題。

訂閱 Spring 新聞通訊

訂閱 Spring 新聞通訊,保持聯絡

訂閱

保持領先

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

瞭解更多

獲取支援

Tanzu Spring 透過一項簡單的訂閱,為 OpenJDK™、Spring 和 Apache Tomcat® 提供支援和二進位制檔案。

瞭解更多

即將舉行的活動

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

檢視全部