javac 足以构建 OSGi 包吗?
要使用 javac 等工具从源代码生成包,您需要为其提供线性类路径。不幸的是,它在某些情况下不起作用,从 OSGi 的角度来看仍然完全合法:
其中嵌入 JAR 的依赖项;
不同依赖项包含相同的包。
由于 javac 不理解 OSGi 元数据,因此我无法简单地了解类路径中的依赖项。似乎有必要采用更精细的包粒度方法。
人们如何在自动化流程(持续集成)中使用 OSGi 来解决这个问题?奇怪的是,如果您有要放入的类/内部 JAR,网络上有很多关于如何创建捆绑 JAR(创建元数据、创建 JAR)的资源,但关于如何实际编译这些类的内容却很少。
让我们举个例子:我的包需要另外两个包来编译,两者都包含 Xerces 作为嵌入式 JAR,但有两个不同且不兼容的版本。这不是问题,因为其中只有一个导出了我的捆绑包依次导入的一些 xerces 包。也许这不是一个非常干净的情况,但在 OSGi 容器中“合法”发生的事情可能没有问题。
现在,我该如何编译它?我无法将两个依赖项放入我的类路径中(javac 无法找到嵌入式 Xerces JAR),我也无法展平(两个版本的 Xerces 将发生冲突,也许未导出的版本会是第一个) 。如果唯一的解决方案是在包级别而不是完整的包级别创建“类路径”,则 javac 根本不可用。
To produce a bundle from source using a tool such as javac, you need to provide it with a linear classpath. Unfortunately, it won't work in some situations still perfectly legal from an OSGi point of view:
dependencies with embedded JAR in them;
same packages contained by different dependencies.
Since javac doesn't understand OSGi metadata, I won't be able to simply but the dependencies in a classpath. A finer package grained approach seems necessary.
How this problem is addressed by people using OSGi in an automated process (continuous integration)? Strangely, there is a lot of resources on the web on how to create bundle JAR (creating the metadata, creating the JAR) provided you have the classes/inner JAR to put inside, but very few things on how actually get theses classes compiled.
Let's take an example: my bundle need two other bundles to compile, both contains Xerces as an embedded JAR but in two different and incompatible versions. It is not a problem since only one of them export some packages of xerces my bundle is in turn importing. Not a very clean situation maybe but something that could "legaly" happen in an OSGi container without problem.
Now, how can I compile it ? I can't put the two dependencies in my classpath (the embedded Xerces JAR won't be found by javac), I can't flatten then either (the two versions of Xerces will collide and maybe the one not exported would be first). If the ony solution is to create a "classpath" on a package level, not on a full bundle scale, javac is not usable at all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Java 类由 javac 编译,这意味着:为了编译包,您必须在类路径中包含您依赖的所有包。
你是对的,javac 不支持 OSGi。因此,您需要其他自动化工具。
要了解这是如何实现的,您可以查看实现 OSGi R4 服务平台的 Felix 。
OSGi 包通常由 ant 或 maven 打包,Eclipse 也支持它们。
使用 Eclipse,在项目设置选项卡“插件依赖项”中配置依赖项。
如何在 Eclipse 中设置 OSGi 项目的一个很好的例子是 Sherlog
Java classes are compiled by javac, this implies: in order to compile a bundle you have to include all bundles you depend on in the classpath.
You're right that javac isn't OSGi aware. So you need other tools for automation.
To see how this is accomplished you could look into Felix which implements an OSGi R4 Service Platform.
An OSGi bundle is usually packed by ant or maven, which are supported by eclipse.
Using eclipse the dependencies are configured in the projects settings tab "plugin-dependencies".
A good example how to setup an OSGi project in eclipse is Sherlog
将任何 java 文件构建为类文件都需要 Javac。 OSGI 捆绑包就像任何其他 java 捆绑归档一样,是类文件的集合和清单,其中 JAR 文件内有几个强制性的自解释条目。
如果您希望您的捆绑包正常运行,那么它的所有依赖项都必须得到满足。如果您希望导出其某些服务或类,那么您还必须以类似的方式声明它们。一旦您理解了这些概念,存档中的 OSGI 特定标头就有意义了。
Javac is required to build any java file into a classfile. OSGI bundles are like any other java bundling archive a collection of class files and a manifest which several mandatory self explanatory entries inside a JAR file.
If you want your bundle to function correctly then it must have all its dependencies satisfied. If you wish to export some of its services or classses then you must also declare these in a similar fashion. Once you understand the concepts the OSGI specific headers within the archive make sense.