将现有的 JAR 转换为 OSGi 包

Convert existing JAR to OSGi-bundle

我有一个 JAR 文件需要转换为 OSGi 包。 我没有 JAR 文件的原始源代码。

我尝试使用以下答案:How to create OSGi bundle from jar library?

但是,它们已经过时了。

编辑:我需要转换几个,但是固定数量的罐子。

选项 1 - 使用 bnd-platform 来构建您的 OSGi 包,当需要频繁的 additions/updates 罐子时,或者当你可以从 Maven 存储库中检索你的依赖项

我们使用 bnd-platform (I am also the author) to manage third party dependencies and create OSGi bundles from them. You can use it with both dependencies retrieved from Maven repositories and local Jars (see the README). If you regularly add or update your dependencies I would suggest that you try bnd-platform. It's a plugin for Gradle, you can easily start with this template - 只需添加您的 Jar 文件并提供项目自述文件中描述的配置(包符号名称、版本)和 运行 gradlew bundles.

选项 2 - 使用 bnd 来构建您的 OSGi 包,当您使用一次或 additions/updates 很少使用时

如果您只执行一次或很少执行此过程,从现有 Jar 创建 OSGi 包的简单方法是直接使用 bnd on the command line. The only thing you need is Java and the bnd jar. You can use wrap to try to automatically wrap a jar or create a .bnd file with instructions for bnd(例如,仅导出特定包)。

示例 .bnd 文件:

-classpath: lib/trove-2.0.4.jar
-output: gnu.trove-2.0.4.jar
Export-Package: *;-split-package:=merge-last;-noimport:=true
Import-Package: *
Bundle-Version: 2.0.4
Bundle-Name: GNU Trove Collections Plug-in
Bundle-SymbolicName: gnu.trove

调用示例:

java -jar <path to bnd>.jar trove-2.0.4.bnd

不再直接通过网站提供 bnd Jar 下载,一个很好的选择是 download it from Maven Central

Eclipse Bundle Recipe 项目提供了一种基于 Maven 的方法,用于将 OSGi 元数据添加到从 Maven 存储库使用的 JAR。尽管名称它不使用 Eclipse。

它的核心是使用 bnd tool. This tool is like a swiss army knife. It analyzes jars and class files and properly calculate package import and exports. You should use bnd for converting proprietary jars yourself. It's available in Maven Central.

如果您使用 Maven,则可以使用 Maven Bundle 插件将依赖项内联或嵌入到 OSGi 包中:

http://felix.apache.org/documentation/subprojects/apache-felix-maven-bundle-plugin-bnd.html#embedding-dependencies

或者只使用 osgi:install 和 wrap 选项,如下所示。

osgi:install wrap:file:/u01/repository/com/oracle/ojdbc6/11.2.0/ojdbc6-11.2.0.jar

这会将 jar 文件部署为包,您可以在“$fuse_home/data/cache/bundle{id}/version0.0”文件夹下获取包。