osgi 应用程序中的非 osgi 库使用
是否可以将非 osgi 库与 OSGi 应用程序一起使用?
例如,我正在开发一个基于语义的搜索引擎,并且正在使用第三方自然语言处理库(http://wiki.opencog.org/w/RelEx_Dependency_Relationship_Extractor)。
是否可以将这样一个不支持 OSGi 的库(作为几个 jar 文件)与我的 OSGi 应用程序连接起来?
Is it possible to use a non-osgi library with an OSGi application?
For an example, I'm developing a semantic based search engine, and I am using a third party Natural Language Processing library for it (http://wiki.opencog.org/w/RelEx_Dependency_Relationship_Extractor).
Is it possible to interface such a library which doesn't suport OSGi, as a couple of jar files, with my OSGi application?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如之前的答案中所写,如果您想在捆绑包中使用其他库,您有两个选择:
第一种方法更简单,因为您只需将库 jar(及其所有依赖项)复制到捆绑包(例如根目录),然后将它们添加到
Bundle-Classpath
MANIFEST.MF 中的 > 元素(请参阅 这里)。但是,在执行此操作时,您必须记住,此添加的库仅在嵌入它的包中可见(因此库重用受到限制)。您始终可以将此库中的包添加到 MANIFEST.MF 中的Export-package
元素,以使其对其他包可见,但这远非优雅的解决方案(但是它会起作用) )。为了使其对其他捆绑包可见,您应该使用第二种方法,即从库创建一个 OSGi 捆绑包(有一些工具可以帮助您做到这一点,也在 Eclipse 中)。然而,对于更复杂的库,这种方法可能会更困难(因为 OSGi 中的依赖关系和特定的类加载方法)。
因此,如果您只想在一个包中使用该库,我建议使用第一种方法(更容易实现)。如果您想在应用程序的多个捆绑包中使用此库,您应该考虑第二种方法。
As it was written in previous answers you have two options if you want to use additional libraries in your bundles:
The first approach is simpler because you only need to copy library jars (and all its dependencies) to a bundle (e.g. to a root directory) and then add them to
Bundle-Classpath
element inMANIFEST.MF
(see here). However, while doing this you must remember that this added library will be visible only in a bundle in which it is embedded (so library reuse is limited). You could always add packages from this library toExport-package
element inMANIFEST.MF
to make it visible for other bundles but this is far from elegant solution (however it will work).In order to make it visible to other bundles you should use the second approach, i.e. create an OSGi bundle from the library (there are tools which can help you in doing that, also in Eclipse). However, for more complicated libraries this approach may be harder (because of dependencies and specific class loading approach in OSGi).
So if you want to use the library only in one bundle I suggest using the first approach (it is easier to implement). If you want to use this library in many bundles in your application you should consider the second approach.
是的,您可以将外部库嵌入到您的包中,也可以将库包装(“OSGIfy”)为 OSGi 包。对于这两个选项,Pax Construct (http://www.ops4j.org/projects/pax/construct< /a>) 是一个很好的工具。
如果您的外部库本身具有依赖项,请将所有这些依赖项嵌入到单个包中或使用 Pax Construct 将它们传递传递。
如果必须在包装或嵌入之间进行选择,请考虑依赖管理和包的版本控制。如果您需要升级外部库并将其嵌入到您自己的应用程序包中,则始终发布该库和您自己的代码。例如,如果没有激活应用程序包的 2 个版本,也不可能激活库的 2 个版本。
而且...如果您不在 OSGi 环境中工作,您想在应用程序 jar 中添加第三方类吗?那么为什么要在 OSGi 环境中进行呢?
在这种情况下,我个人更喜欢将外部库视为黑匣子,并将库及其依赖项包装在一个包中。
Yes, you can either embed the external library in your bundle or wrap ("OSGIfy") the library as an OSGi bundle. For both options, Pax Construct (http://www.ops4j.org/projects/pax/construct) is a good tool.
If your external library itself has dependencies, embed all of these in a single bundle or use Pax Construct to wrap them transitive.
If have to to choose between wrapping or embedding, consider dependency management and versioning of bundles. If you need to upgrade the external library and it's embedded in your own application bundle, you always release both the library and your own code. It is e.g also not possible to have 2 version of the library active without having 2 version of your application bundle active.
And also... if you are not working in an OSGi environment, would you like to add third-party classes in your application jars? So why do it in an OSGi environment?
I personally prefer in such a case to see the external lib as a black box, and wrap the library and it's dependencies in a single bundle.
是的,这是可能的。您有两个选择:
首先,您可以将外部库中的所有包包含到捆绑包的私有包部分中。它将把所有这些包包含到您的应用程序的 jar 中。
第二个选项是从外部库制作有效的 osgi 包。
Yes, it is possible. You have two options:
Firstly, you can include all packages from external library into private-package section of your bundle. It will include all this packages into jar with your application.
The second option is to make a valid osgi bundle from external library.
对于 Eclipse IDE;
For Eclipse IDE;