如何在 OSGi 容器中的捆绑包之间共享非 OSGi 库?
当尝试共享 Struts 包时,我遇到了这个问题OSGi 容器内的多个包之间。 我想避免在包内重复依赖项并在它们之间引入新的依赖项(通过让一个包导出其内部依赖项以供另一个包共享)。
事实证明,如果您碰巧使用 Maven,那么答案非常简单,如果您没有使用 Maven,您仍然可以使用 Maven 解决方案来学习如何通过其他机制创建相同的结果。
我发现这种方法在我的项目中多次有用,所以我将在这里发布一个示例。
I came across this question when trying to share the Struts packages among multiple bundles inside an OSGi container. I wanted to avoid both duplicating dependencies inside the bundles and introducing a new dependency between them (by having one bundle export its internal dependencies for the other to share).
It turns out the answer is very easy if you happen to use Maven, and if you aren't, you can still use the Maven solution to learn how to create the same result with some other mechanism.
I found this approach useful multiple times during my project, so I'll post an example here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于那些不太热衷于 Maven、无法移植或对 ant/ivy 非常满意的人来说,还有一条额外的途径,
我发现完成所述任务的最简单方法是通过让清单导出每个包并添加一些适当的符号名称/版本。 我已经能够通过 ant 任务(甚至直接命令行)使用 bnd 非常轻松地完成此操作调用)。 还有存储库,其中包含许多流行库的“osgi-ified”版本。 一些库(joda-time)已经附带了正确的 OSGi 清单。
An additional path for those not so keen on maven, unable to port, or perfectly happy with ant/ivy
I've found the that easiest way to accomplish the stated task is to turn the non-OSGi library into an OSGi library by letting the manifest export every package and add on some approriate symbolic names / versions. I've been able to do this VERY easily with bnd via ant tasks (or even direct command line invocation). There are also repositories which contain "osgi-ified" version of many popular libraries. Some libraries (joda-time) are already shipping with correct OSGi manifests.
使用 Maven,可以非常轻松地从任何库创建 OSGi 包。 然而,我认为使用其他机制也可以产生相同的结果。 Maven 解决方案帮助我了解它是如何工作的。
创建捆绑包是通过创建一个以该库作为依赖项的项目,然后使用 maven-bundle-plugin 来自 Apache Felix项目并使用
Export-Package
指令指定库包。 我用它在 OSGi 容器内的捆绑包之间共享 Google Protocol Buffers:如果您也希望将所有传递依赖项都汇总到捆绑包中,请使用插件的
bundleall
目标。该插件识别并尊重依赖项中现有的 OSGi 清单。
您还可以使用捆绑插件来创建清单,并告诉
jar
打包插件(或jar-with-dependencies
内置程序集)通过存档使用该清单部分。 上面链接的插件页面显示了如何做到这一点。Using Maven, it is very easy to create an OSGi bundle from any library. However, I think the same result can be created with other mechanisms, too. The Maven solution helped me understand how it works.
Creating the bundle is done by creating a project which has the library as a dependency and then packaging the project using the maven-bundle-plugin from the Apache Felix project and specifying the library packages with the
Export-Package
instruction. I used this to share Google Protocol Buffers between bundles inside an OSGi container:If you want all transitive dependencies rolled into the bundle, too, use the
bundleall
goal of the plugin.The plugin recognizes and honours existing OSGi manifests in the dependency.
You can also use the bundle plugin to just create the manifest and tell the
jar
packaging plugin (or thejar-with-dependencies
builtin assembly) to use that manifest via the archive section. The plugin's page linked above shows how to do that.