Java OSGI - 处理捆绑包所需的第 3 方 JAR
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1340483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OSGI - handling 3rd party JARs required by a bundle
提问by William
I'm just getting started with OSGI development and am struggling to understand how best to handle dependant JARs.
我刚刚开始使用 OSGI 开发,并且正在努力了解如何最好地处理依赖的 JAR。
i.e. if I'm creating a bundle the likelyhood is that I will need to use a few 3rd party JARs. When I create my bundle JAR to deploy to OSGI, obviously these 3rd party JARs are not included and thus the bundle will not run.
即如果我正在创建一个包,很可能我将需要使用一些 3rd 方 JAR。当我创建要部署到 OSGI 的包 JAR 时,显然这些 3rd 方 JAR 不包括在内,因此包不会运行。
I understand that one option is to turn these JARs into bundles and also deploy them to the OSGI container. However if they only need to be used by the one bundle this doesn't seem ideal.
我知道一种选择是将这些 JAR 转换为包并将它们部署到 OSGI 容器中。但是,如果它们只需要由一个包使用,这似乎并不理想。
What is the best solution to this? Can the JARs be embedded within the bundle JAR and if so is this a reasonable approach?
对此的最佳解决方案是什么?JAR 是否可以嵌入到包 JAR 中,如果是这样,这是一种合理的方法吗?
采纳答案by Roland Schneider
I would almost always bundle each jar separately. OSGi itself is meant for modularization and you take the whole system ad absurdum by not doing this.
我几乎总是将每个罐子分开打包。OSGi 本身就是为了模块化,如果不这样做,整个系统就会变得荒谬。
If you want to convert JARs into bundles you might want to use the BND Tool written by Peter Kriens. But first I would suggest you look for the bundle in the SpringSource Enterprise Bundle Repositoryif they haven't already done the work for you.
如果您想将 JAR 转换为包,您可能需要使用Peter Kriens 编写的BND 工具。但首先我建议您在SpringSource Enterprise Bundle Repository 中查找 bundle,如果他们还没有为您完成工作。
回答by Pavol Juhos
It is possible to embed non-OSGi dependencies into the bundle.
可以将非 OSGi 依赖项嵌入到包中。
An easy way to do this is to use Maven to manage your dependencies and Maven Bundle Pluginto build your bundle. Take a look at the <Embed-Dependency>
and <Embed-Transitive>
instructions of the Maven Bundle Plugin described in the section Embedding dependenciesof the plug-in documentation page.
一个简单的方法是使用 Maven 来管理您的依赖项,并使用Maven Bundle Plugin来构建您的包。查看插件文档页面的嵌入依赖项部分中描述的 Maven Bundle Plugin<Embed-Dependency>
和<Embed-Transitive>
说明。
As Roland pointed out this is not an ideal solution with respect to the intentions of OSGi, i.e. modularization and reuse of individual modules. However it might be pragmatic solution for time being until the 3rd-party dependencies can be converted into OSGi bundles.
正如 Roland 所指出的,就 OSGi 的意图而言,这不是一个理想的解决方案,即单个模块的模块化和重用。然而,在第 3 方依赖项可以转换为 OSGi 包之前,这可能是暂时的实用解决方案。
回答by William
You can include a third party jar inside your bundle by adding the third party jar to the root directory of the bundle jar file and then adding a bundle classpath header to the bundle's manifest, e.g.:
您可以通过将第三方 jar 添加到包 jar 文件的根目录,然后将包类路径标头添加到包的清单中,在包中包含第三方 jar,例如:
Bundle-ClassPath: .,my3rdparty.jar
If you want to place third party jar to subdirectory, specify the path without using heading ./, e.g
如果要将第三方 jar 放置到子目录,请指定路径而不使用标题./,例如
Bundle-ClassPath: .,lib/my3rdparty.jar # (not ./lib/my3rdparty.jar)
回答by Ed Ost
This thread is a bit old, but I wanted to point out one of the limitations of embedding dependencies. Recall that dependencies are at the jar level, but when you export packages some may need to come from the embedded dependencies. If this happens, you will end up with duplicate classes, one set inline in the top level bundle and another in the embedded jar. Of course, you can inline the entire embedded jar, but before you know it this propagates across your entire dependency chain. This is just one of the problems that Roland and others refer to.
这个线程有点旧,但我想指出嵌入依赖项的限制之一。回想一下依赖项是在 jar 级别,但是当您导出包时,一些可能需要来自嵌入的依赖项。如果发生这种情况,您最终会得到重复的类,一个在顶级包中内联,另一个在嵌入式 jar 中。当然,您可以内联整个嵌入的 jar,但在您知道它之前,这会在您的整个依赖链中传播。这只是罗兰等人提到的问题之一。
回答by shreyas
Here is an example if you are using the Maven Bundle Plugin.
如果您使用的是Maven Bundle Plugin,这里是一个示例。
Note: This plugin automatically imports packages that your dependencies need. This may or may not be a problem for you. Thankfully, you can suppress the packages you don't really need to import (see below).
注意:此插件会自动导入您的依赖项所需的包。这对您来说可能是也可能不是问题。值得庆幸的是,您可以抑制不需要导入的包(见下文)。
<Import-Package>
<!-- this was imported by one of the dependencies; I don't really need it -->
!org.apache.Hymanrabbit.test,
*
</Import-Package>
<Include-Resource>
lib/concurrent-1.3.4.jar,
lib/Hymanrabbit-core-2.6.5.jar,
lib/Hymanrabbit-spi-2.6.5.jar,
lib/Hymanrabbit-spi-commons-2.6.5.jar,
lib/lucene-core-3.6.0.jar,
lib/tika-core-1.3.jar
</Include-Resource>
<Bundle-ClassPath>
.,
concurrent-1.3.4.jar,
Hymanrabbit-core-2.6.5.jar,
Hymanrabbit-spi-2.6.5.jar,
Hymanrabbit-spi-commons-2.6.5.jar,
lucene-core-3.6.0.jar,
tika-core-1.3.jar
</Bundle-ClassPath>
回答by capulet
Can we use OSGI to override the bootstrap classloader jars loaded during runtime, like if we wanted to override JAXP1.4.5 available with Java7 to JAXP1.6, there is -Dendorese feature to override the default API to upgraded API. Can we able to do this thing with the help of OSGI.
我们可以使用 OSGI 来覆盖在运行时加载的引导类加载器 jars,就像如果我们想将 Java7 提供的 JAXP1.4.5 覆盖到 JAXP1.6,有 -Dendorese 功能将默认 API 覆盖到升级后的 API。我们能否在 OSGI 的帮助下做这件事。