1. 什么是 Bundlor?
Bundlor 是一个用于生成 OSGi 包(Bundle)的工具,它可以根据项目的依赖和配置自动生成符合 OSGi 规范的 MANIFEST.MF
文件。Bundlor 通常与 Maven 或 Gradle 等构建工具集成,简化 OSGi 包的构建过程。
2. 什么是 OSGi Profile?
OSGi Profile 是一个配置文件,用于定义 OSGi 包的元数据(如 Bundle-SymbolicName
、Export-Package
、Import-Package
等)。Bundlor 使用这个文件来生成 MANIFEST.MF
文件。
3. 使用 Bundlor 的 OSGi Profile
(1) 创建 OSGi Profile
OSGi Profile 通常是一个 .bundlor
文件,内容如下:
Bundle-SymbolicName: com.example.mybundle
Bundle-Version: 1.0.0
Export-Package: com.example.mypackage
Import-Package: org.osgi.framework;version="[1.8,2)"
(2) 配置 Maven 使用 Bundlor
在 pom.xml
中添加 Bundlor 插件:
<build>
<plugins>
<plugin>
<groupId>org.eclipse.virgo.bundlor</groupId>
<artifactId>bundlor-maven-plugin</artifactId>
<version>1.1.2.RELEASE</version>
<configuration>
<manifestTemplate>src/main/resources/META-INF/MANIFEST.MF.template</manifestTemplate>
<osgiProfile>src/main/resources/osgi.profile</osgiProfile>
</configuration>
<executions>
<execution>
<goals>
<goal>manifest</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
(3) 生成 OSGi Bundle
运行以下 Maven 命令生成 OSGi Bundle:
mvn clean package
4. 示例项目结构
my-osgi-project/
├── pom.xml
├── src/
│ ├── main/
│ │ ├── java/
│ │ │ └── com/example/mypackage/
│ │ │ └── MyClass.java
│ │ └── resources/
│ │ ├── META-INF/
│ │ │ └── MANIFEST.MF.template
│ │ └── osgi.profile
5. 总结
通过使用 Bundlor 和 OSGi Profile,可以轻松生成符合 OSGi 规范的 MANIFEST.MF
文件,简化 OSGi 包的构建过程。Bundlor 的集成使得 OSGi 开发更加高效和规范。
更多详细信息,请参考 Bundlor 官方文档:https://www.eclipse.org/virgo/documentation/bundlor-documentation/。
When managing and transforming as many bundles as are included in the the SpringSource Enterprise Bundle Repository, it becomes very difficult to remember what packages are boot delegated, exported from the system bundle, or from other bundles in your system. This information is important because you probably don’t want to import packages that are boot delegated, you probably do want to import system bundle packages at “0”, and you want to define custom imports for all others. Remembering which packages are in each of these categories ends up being a bit error prone and defining template entries for them can be time-consuming.
With the release of 1.0.0.M4, Bundlor has gained the ability to take an OSGi profile as input and automatically add template entries for boot delegated packages and system bundles. This input is taken as a properties file and can be used with the command line, ANT, and Maven.
OSGi Profile
An OSGi profile defines the packages that are exported from the system bundle and the packages that are delegated to the boot class loader. This profile isn’t a particular file, but rather two properties that are well known to an OSGi runtime. The property org.osgi.framework.system.packages defines the packages exported from the system bundle and org.osgi.framework.bootdelegation defines the packages that are boot delegated. For more information on the syntax of the values of these properties please see the OSGi specification §3.8.3 and §3.8.5.
For Bundlor, these properties are defined in a standard .properties file such as the following.
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.,
…
Effects
As an example of how this works I’ve taken a slightly modified Bundlor template for Apache Log4J. The template I’m using looks like this
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
When run against Bundlor without a profile I get the following warnings
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.
To satisfy these warnings, I could add a number of Import-Template entries. Instead, I’ll run Bundlor again with the dm Server OSGi profile. This profile exports each of these packages from the system bundle. This run returns with no warnings and imports each of these packages at version=“0”. This bundle did not depend on any package that is boot delegated so there are no ignore imports in our manifest.
Usage
ANT
The ANT task has gained a new optional osgiProfilePath attribute.
<bundlor:bundlor
bundlePath=“
b
a
s
e
d
i
r
/
o
r
g
.
s
p
r
i
n
g
f
r
a
m
e
w
o
r
k
.
i
n
t
e
g
r
a
t
i
o
n
.
j
a
r
"
o
u
t
p
u
t
P
a
t
h
=
"
{basedir}/org.springframework.integration.jar" outputPath="
basedir/org.springframework.integration.jar"outputPath="{basedir}/target/org.springframework.integration.jar”
bundleVersion=“1.0.2.BUILD-
t
i
m
e
s
t
a
m
p
"
o
s
g
i
P
r
o
f
i
l
e
P
a
t
h
=
"
j
a
v
a
6
−
s
e
r
v
e
r
.
p
r
o
f
i
l
e
"
m
a
n
i
f
e
s
t
T
e
m
p
l
a
t
e
P
a
t
h
=
"
{timestamp}" osgiProfilePath="java6-server.profile" manifestTemplatePath="
timestamp"osgiProfilePath="java6−server.profile"manifestTemplatePath="{basedir}/template.mf”/>
Maven
The Maven plugin has gained a new optional osgiProfilePath configuration.
com.springsource.bundlor com.springsource.bundlor.maven 1.0.0.M2 bundlor transform java6-server.profile ... ...Command Line
The command line interface has gained a new optional -p, –profile parameter for both the manifest and transform executions.
bin/bundlor.sh transform
-b com.springsource.org.apache.log4j.jar
-m com.springsource.org.apache.log4j.mf
-p java6-server.profile
The ability to read in an OSGi profile as input greatly simplifies content of manifest templates. If there are more areas that you can think of improvements, please open an issue or vote on an existing one at the Bundlor JIRA.
comments powered by Disqus
当管理和转换SpringSource Enterprise Bundle信息库中包含的所有包时,很难记住是从系统包还是从系统中的其他包引导启动,导出了哪些包。此信息很重要,因为您可能不想导入引导委派的软件包,您可能希望导入系统捆绑软件包为“ 0”,并且想为所有其他软件包定义自定义导入。记住每个类别中的哪个软件包最终都容易出错,为它们定义模板条目可能很耗时。
在1.0.0.M4发行版中,Bundlor获得了将OSGi配置文件作为输入并自动为启动委托包和系统捆绑添加模板条目的功能。此输入被视为属性文件,可以与命令行,ANT和Maven一起使用。
OSGi配置文件
OSGi概要文件定义了从系统捆绑包导出的软件包以及委派给引导类加载器的软件包。此配置文件不是特定文件,而是OSGi运行时众所周知的两个属性。属性org.osgi.framework.system.packages定义从系统捆绑包导出的软件包,而org.osgi.framework.bootdelegation定义由启动委托的软件包。有关这些属性的值的语法的更多信息,请参见OSGi规范§3.8.3和§3.8.5。