且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Netbeans和Karaf中开发OSGI捆绑包时如何管理依赖项?

更新时间:2023-11-18 17:27:04

在使用OSGi时,编译时和部署时依赖项之间的不匹配确实是一个问题.

The mismatch between dependencies at compile time and at deployment time is indeed a problem when working with OSGi.

您需要知道的是它们之间如何相互联系.在编译时,您具有maven依赖项.当您构建项目时,maven捆绑插件会为您创建必要的Import-Package和Export-Package语句.在大多数情况下,这都是行之有效的,您应该避免在那里手动调整太多.

What you need to know is how these relate to each other. At compile time you have the maven dependencies. When you build your project the maven bundle plugin creates the necessary Import-Package and Export-Package statements for you. Most of the time this simply works and you should avoid tuning too much there by hand.

因此,在清单生成之后,清单会指定所需的结果包,但这不会自动确保满足依赖关系.因此,当您将捆绑软件部署到karaf时,您会得到有关缺少依赖项的错误.这是正常的.

So after the build the Manifest specifies what the resulting bundle needs but this does not automatically make sure the dependencies are met. So when you deploy the bundle to karaf you get the errors about missing dependencies. This is normal.

现在,您需要安装其他满足这些依赖关系的捆绑软件.通常,您可以简单地安装maven依赖项的jar(如果它们是捆绑包).但是,这并不总是有效.因此,基本上,您需要安装捆绑软件,然后检查是否满足要求.

Now you need to install other bundles that fulfil these dependencies. Often you can simply install the jars of your maven dependencies (if they are bundles). This does not always work though. So basically you install the bundles and check if the requirements are met now.

使用karaf的好处是,许多较大的依赖项(例如cxf)已经可以用作功能.因此,***先尝试通过安装可用功能来获取依赖项,然后再尝试逐捆绑安装它们.

The nice thing with karaf is that many bigger dependencies like cxf are already available as features. So it is a good idea to first try to get the dependencies by installing available features before you try to install them bundle by bundle.

因此,这有助于您正确地建立依赖关系.然后,对于生产",***的解决方案是创建自己的功能文件,在其中引用找到的包.

So this helps you in getting your dependencies right. Then for "production" use the best solution is to create your own feature file where you refer to the bundles you found.

虽然一开始使用deploy dir安装捆绑软件看起来不错,但这不是一个好的解决方案.***使用mvn:karaf提供的url语法直接从maven安装捆绑软件.

While installing bundles using the deploy dir seems nice at first it is not a good solution. Better install the bundles directly from maven using the mvn: url syntax karaf provides.

关于嵌入依赖性.有时这是一个很好的解决方案,但通常只会使情况变得更糟,因为它可能导致难以解决的软件包使用冲突.因此***不要嵌入任何东西.

Regarding embedding dependencies. Sometimes it is a good solution but often it only makes things worse as it can lead to package use conflicts which are hard to solve. So better do not embed anything if it is possible.