如何从 eclipse/osgi 应用程序中包含对 jar 文件的依赖关系?

2022-01-16 00:00:00 plugins eclipse-plugin java eclipse osgi

我创建了一个 Eclipse 4 应用程序,我需要一个 jar 作为我的应用程序的一部分提供功能(这可以是任何东西,例如 log4j 以使其变得微不足道).
我将 jar 添加为项目类路径的一部分(Right Click->Configure Build Path),但在运行时我的服务因 ClassNotFound 而失败错误(我猜来自 OSGI?).
无论如何搜索这个结果,至少据我了解,我应该将 jar 添加为另一个 Plugin 的一部分,并创建从我的应用程序/服务到这个新插件的依赖项.
IE.我从现有的 JAR 档案中创建了一个插件项目.
这次设置成功了.
因此,如果我理解这一点,在为 Eclipse/OSGi 开发时,我们不应该直接在类路径中添加 jars,而是通过插件添加它们(为什么?).
问题:如果到目前为止我是正确的,那么在开发项目时包含 jars 的标准做法是什么?
从现有的 JAR 档案中定义/创建一个 Plugin Project 并添加 all 所需的第三方库,或 根据需要使用不同的插件项目jar or 可能是别的什么???
对不起,如果我的术语不准确.我是 OSGi 和 Eclipse 编程新手

I created an Eclipse 4 application and I needed a jar offering a functionality as part of my application (this could be anything e.g. log4j to make it trivial).
I added the jar as part of my project's classpath (Right Click->Configure Build Path) but on runtime my service failed with a ClassNotFound error (from OSGI I guess?).
Anyway searching this it turned out, at least as I have understand it, that I should add the jar as part of another Plugin and create a dependency from my application/service to this new plugin.
I.e. I created a Plugin Project from Existing JAR archives.
This time the setup worked.
So if I understand this, when developing for Eclipse/OSGi we should not add jars in the classpaths directly but add them via plugins (why?).
Question: If I am correct so far, what is the standard practice to include jars when developing a project?
Define/Create one Plugin Project from existing JAR archives and add all the required third party libraries needed there, or have a different plugin project per needed jar or something else perhaps???
Sorry if my terminology is not accurate. I am new in OSGi and Eclipse programming

注意:在谈到 jars 时,我并不是指其他 OSGi 服务.我指的是使用应用程序的许多部分都需要的现成、可靠的第三方库的规范.例如.log4j 或 xml 解析库或 apache commons

Note: When talking about jars I am not refering to other OSGi services. I am refering to the norm of using ready, reliable third party libraries that would be needed by many parts of an application. E.g. log4j or an xml parsing library or apache commons etc

推荐答案

对于运行时来说,它始终是 Manifest 和那里的头文件,它们控制着你的包类路径中的内容.可以通过三种方式访问​​ jar:

For the runtime it is always the Manifest and the headers there that control what is in your bundle classpath. There are three ways to get access to a jar:

  1. 导入包标头.这是推荐的方式.您为每个需要的包定义一个导入.您要访问的 jar 必须作为包部署在运行时中.它还需要导出所有需要的包.

  1. Import-Package header. This is the recommended way. You define one import per package you need. You jar you want to access has to be deployed in the runtime as a bundle. It also needs to export all needed packages.

需要捆绑包.这是访问捆绑包的另一种方式.您定义所需捆绑包的 ID 并查看它导出的所有包.由于 Require-Bundle 将您更紧密地绑定到另一个捆绑包,因此应该首选 Import-Package 方式.

Require-Bundle . This is another way to access bundles. You define the id of the bundle you need and see all packages it exports. As Require-Bundle binds you more closely to the other bundle the Import-Package way should be preferred.

捆绑类路径.这允许将 jar 添加到您嵌入到您自己的包中的类路径中.只有当其他方法不起作用时,这才应该是最后的手段.将其与其他方法混合使用时,您可能会遇到令人讨厌的类加载问题.

Bundle-Classpath . This allows to add jars to your classpath that you embed into your own bundle. This should only be a last resort when the other way do not work. You can have nasty classloading issues when mixing this with the other methods.

您可以在 maven Central 中找到许多预构建的捆绑包.今天的许多 jars 已经包含一个 OSGi 清单.对于不正确的情况,许多 jar 被 servicemix 重新打包为捆绑包.请参阅 groupId:org.apache.servicemix.bundles.还有 spring bundle 存储库,您可以在其中找到更多信息.

You can find many pre built bundles in maven central. Many jars today already contain an OSGi manifest. For the cases where this is not true many jars are repackaged as bundles by servicemix. See groupId: org.apache.servicemix.bundles. There is also the spring bundle repository where you find some more.

下面我列出了一些您可能想阅读的资源:

Below I listed some resources you might want to read:

http://www.aqute.biz/Blog/2007-02-19

http://wiki.osgi.org/wiki/Import-Package

http://wiki.osgi.org/wiki/Require-Bundle

http://www.vogella.com/blog/2009/03/27/required-bundle-import-package/

相关文章