領先一步
VMware 提供培訓和認證,以加速您的進展。
了解更多最近 Bundlor 的 M3 里程碑版本已經發佈 (論壇公告)。此里程碑版本新增了對屬性替換和版本擴展的支援。這篇部落格文章說明如何使用這些新功能來提升產生 Manifests 的品質。
Bundlor 現在可以用於將任何屬性值替換到您的 Manifest 範本中。
Bundle-Name: ${name} Bundle-Description: Test bundle using new version of Kernel at ${com.springsource.kernel} Import-Template: com.springsource.kernel.*;version="${com.springsource.kernel}"
此語法讓您可以為 ${name}
和 ${com.springsource.kernel}
指定屬性佔位符,並在執行時將它們替換為實際值。這些值的傳遞方式取決於所使用的 Bundlor 前端。
當 Bundlor 從命令列執行時,它會使用所有可用的系統屬性,這不包括任何環境變數。命令列腳本會傳遞透過 -D
傳入的任何變數,因此以下指令將為 ${com.springsource.kernel}
屬性提供值 '2.0.0.RELEASE',並為 ${name}
屬性提供值 'Kernel test bundle'。
bundlor.sh \ transform \ --bundle ./org.springframework.integration.jar \ --manifest ./template.mf \ --outputfile ./target/org.springframework.integration.jar \ -Dcom.springsource.kernel=2.0.0.RELEASE \ -Dname="Kernel test bundle"
若要從 Maven 建置將屬性傳遞給 Bundlor,您應該在 Maven 專案檔案的 project 元素下定義新的 (或新增至現有的) properties 元素。
<properties> <name>${project.name}</name> <com.springsource.kernel>2.0.0.RELEASE</com.springsource.kernel> </properties>
這將執行 Bundlor,其中 ${com.springsource.kernel}
的替換值為 '2.0.0.RELEASE',而 ${name}
的替換值為 Maven 專案的名稱。
從 Ant 呼叫 Bundlor 任務時,可以在任務上提供額外的屬性,以傳入將傳遞給 Bundlor 的 Ant Property Set。
<bundlor:bundlor> bundlePath="${basedir}/org.springframework.integration.jar" outputPath="${basedir}/target/org.springframework.integration.jar" bundleVersion="2.0.0.BUILD-${timestamp}" manifestTemplatePath="${basedir}/template.mf"> <propertyset> <property name="name" value="${ant.project.name}"/> <property name="com.springsource.kernel" value="2.0.0.RELEASE"/> </propertyset> </bundlor:bundlor>
這將執行 Bundlor,並為 ${com.springsource.kernel}
提供替換值 '2.0.0.RELEASE',為 ${name}
提供 Ant 專案名稱的替換值。
屬性替換可以發生在 Manifest 範本值的任何位置。它甚至可以用於替換 'Import-Template' 版本。單獨來看,匯入中的單個版本不是非常有用,因為它相當受限制,但是自動將版本擴展為版本範圍是有用的。可以在冒號後給定版本擴展模式,該模式將應用於版本以產生版本範圍。
Import-Template: com.springsource.kernel.*;version="${com.springsource.kernel:[=.=.=.=, +1.0.0)}", org.apache.commons.logging.*;version="${org.apache.commons.logging:[=.=.=.=, =.=.+1)}"
版本擴展適用於持有合法 OSGi 版本號碼的變數。根據該版本號碼,擴展會分為 4 個部分:主要版本號、次要版本號、微版本號和限定詞。如果提供的屬性不是有效的 OSGi 版本,則 Bundlor 將失敗並拋出例外,指出提供的屬性不是有效的版本字串。
此版本號碼的前三個部分可以在擴展中替換如下: | |
= | 等於變數中的值 |
[+/-]n | 按此數量調整變數中的值 |
n | 將此值替換為變數中的值。這通常僅用於輸入 0 |
第四個部分(限定詞)只能採用以下替換: | |
= | 等於變數中的值 |
任何合法的限定詞值。 | 將此值替換為變數中的值 |
${com.springsource.kernel}
和給定的版本 1.2.0 產生的版本範圍將為 [1.2.0, 2.0.0)。這表示將使用從 1.2.0 到(但不包括)2.0.0 的最高可用版本。對於 ${org.apache.commons.logging}
和給定的版本 1.4.0,產生的版本範圍將為 [1.4.0, 1.4.1),這更具限制性,因為它僅允許對版本 1.4.0 的限定詞進行更改,而不允許使用 1.4.1.xxx 及更高版本。如果模式將用於多個匯入,則可以使用名稱指定它並多次使用。範本 Manifest 中使用了一個新的標頭來指定命名的版本擴展模式。
Import-Template: org.apache.commons.codec.*;version="${org.apache.commons.codec:apache}", org.apache.commons.logging.*;version="${org.apache.commons.logging:apache}", org.hibernate.*;version="${org.hibernate:hibernate}" org.myorg.*;version="${org.myorg:(=.=.=.=, =.+1.0.=]}" Version-Patterns: apache;pattern="[=.=.=.=, +1.0.0)", hibernate;pattern="[=.=.=.=, =.=.+1)"
這顯示了定義多個模式並將其用於多個匯入。apache 模式將給出從提供的版本到但不包括下一個主要版本的版本範圍。hibernate 模式將給出從提供的版本到但不包括下一個微版本的版本範圍。還有一個使用內嵌版本擴展模式定義的匯入,它可以與同一個範本中的命名模式一起正常使用。內嵌模式顯示了更不常見的用法。它將建立從提供的版本(但不包括)到下一個次要版本(包括)的版本範圍,並具有相同的限定詞。
將這些技術結合使用,可以產生可靠的 manifests。正確使用版本擴展將確保產生的 manifests 具有足夠的限制性以阻止不必要的連線,但又足夠靈活以允許進行細微的更改。在編寫範本 Manifest 時,應針對每個相關的 bundle 採取務實的方法,因為並非所有 bundle 都以相同的方法進行版本控制。