如何从eclipse / osgi应用程序中包含对jar文件的依赖关系? [英] How to include a dependency to a jar file from an eclipse/osgi application?

查看:143
本文介绍了如何从eclipse / osgi应用程序中包含对jar文件的依赖关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个Eclipse 4应用程序,我需要一个 jar 提供一个功能作为我的应用程序的一部分(这可以是任何东西,例如 log4j 使它变得微不足道)。

我添加了 jar 作为我项目类路径的一部分(右键单击 - >配置构建路径),但在运行时我的服务失败,一个 ClassNotFound 错误(来自OSGI我猜?)

无论如何,搜索这一点,原来,至少我已经明白了,我应该添加jar作为另一个插件的一部分,并从我的应用程序/服务创建依赖到这个新插件。

即可我从现有的JAR档案创建了一个插件项目

这次安装工作正常。

所以如果我明白这一点开发Eclipse / OSGi,我们不应该直接在类路径中添加 jars ,而是通过插件添加它们(为什么?)。

问题: / strong>如果我到目前为止是正确的,在开发项目时,包括 jars 的标准做法是什么?

定义/创建一个现有JAR存档的插件项目并添加全部需要的第三方库, code> jar 其他可能的东西

对不起,如果我的术语不准确。我是OSGi和Eclipse编程中的新功能



注意:谈论 jars 我没有参考其他OSGi服务。我参考了应用程序的许多部分需要使用准备好,可靠的第三方库的规范。例如。 log4j 或一个xml解析库或 apache commons

解决方案

对于运行时,它始终是清单,并且那里的头部可以控制你的bundle类路径中的内容。有三种访问jar的方法:


  1. Import-Package标题。这是推荐的方法。您需要为每个包定义一个导入。您想要访问的jar您必须在运行时部署为捆绑包。它还需要导出所有需要的软件包。


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


  3. Bundle-Classpath。这可以将jar添加到您嵌入到自己的包中的类路径中。这只能是最后的手段,而另一种方式不行。您可以将其与其他方法混合使用时遇到讨厌的类加载问题。


您可以在maven中找到许多预先构建的bundle中央。许多罐子今天已经包含OSGi清单。对于不是这样的情况,许多罐子被servicemix重新打包成捆绑包。请参阅groupId:org.apache.servicemix.bundles。还有一个spring bundle存储库,你可以在这里找到一些。



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



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



http://维基百科,自由的百科全书rel =noreferrer> http://wiki.osgi.org/wiki/Require-Bundle



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


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

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

解决方案

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. 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.

  2. 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.

  3. 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.

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/

这篇关于如何从eclipse / osgi应用程序中包含对jar文件的依赖关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆