osgi:导入部分存在于包内的包

发布于 2024-12-01 16:36:25 字数 244 浏览 1 评论 0原文

我有一个包 XYZ,它存在于 2 个包 A 和 B 中:

bundle A
 package X.Y.Z 
  class Class1

bundle B
 package X.Y.Z 
  class Class2

包 B 导出包 XYZ Bundle A 导入包 XYZ 并得到一个异常,即找不到自己的类 Class1。应该有效吗?

我使用 glassfish 3.1 和 felix

I have a package X.Y.Z that exists in 2 bundles A and B:

bundle A
 package X.Y.Z 
  class Class1

bundle B
 package X.Y.Z 
  class Class2

Bundle B exports package X.Y.Z.
Bundle A imports package X.Y.Z and gets an exception that his own class Class1 is not found. Should it work?

I use glassfish 3.1 with felix

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沉溺在你眼里的海 2024-12-08 16:36:25

不,它不应该工作。如果您导入包 XYZ,则该导入将优先于包自身的内部内容使用。

更一般地说,您遇到了一个称为“拆分包”的问题。包应该是连贯的并由单个包导出,而不是分散在多个包中。您应该重构您的包内容,以便属于包 XYZ 的所有类都存在于单个包中。

No it should not work. If you import package X.Y.Z then that import will be used in preference to the bundle's own internal contents.

More generally, you have a problem known as split packages. Packages should be coherent and exported by a single bundle, not smeared across multiple bundles. You should refactor your bundle contents so that all classes belonging to package X.Y.Z are present in a single bundle.

岁吢 2024-12-08 16:36:25

再说一次,尼尔是绝对正确的。但是,有时供应商出于某种原因会拥有多个包含相同包的 .jar 文件。当他们想要提供小型 .jar 文件以谨慎实现其产品时,有时会这样做。这样做的一个例子是,如果他们的 EDI 文档的文本处理算法与 xml 文档的文本处理算法不同。在此示例中,他们可以选择创建两个 .jar(版本 1 和 2)文件,其中包含“badlyPlannedImplementation.util”,其中包含不同的实现类。就我个人而言,我只遇到过几次这种情况,但问题是你如何处理它?

当您遇到以下问题:您有两个导出相同包的 .jar 文件并且您希望访问这两个包类时,您可以使用一种称为“着色”的机制。阴影是指您获取这两个包并将它们的内容收集到另一个 .jar 文件包中。这过去是由一个名为“maven-shade-plugin”的 Maven 插件完成的,但现在该功能是 maven-bundle 插件的一部分。

首先,创建一个新项目,我们将其命名为“badlyPlannedImplementationShaded”。然后,在您的项目中创建一个 pom.xml 文件。在依赖项部分中,包含您尝试一起着色的两个 .jar 文件的依赖项。

然后,将以下内容添加到您的构建部分。

<plugin>
   <groupId>org.apache.felix</groupId>
   <artifactId>maven-bundle-plugin</artifactid>
   <version>2.1.0</version>
   <extensions>true</extensions>
   <configuration>
      <instructions>
         <Bundle-Version>${project.version}</Bundle-Version>
         <Export-Package>
            badlyPlannedImplementation.util;version="1",
            badlyPlannedImplementation.util;version="2"
         </Export-Package>
      </instructions>
   </configuration>
</plugin> 

执行此操作将创建一个新包,其中包含一个 util 包,该包包含您尝试使用的两个 .jar 文件中的所有类。

我希望这有帮助!

Again, Niel is absolutely correct. But, sometimes vendors, for whatever reason will have multiple .jar files containing the same packages. This is sometimes done when they want to provide small .jar files for discreet implementations of thier product. An example of why this may be done is if they have a text-processing algorithm for an EDI document that is different than a text processing algorithm for an xml document. In this example, they may choose to create two .jar (versions 1 and 2) files containing "badlyPlannedImplementation.util" containing the different implementationing classes. Personally, I've only run into this a couple of times, but the question is how do you handle it?

When you run into the issue where you have two .jar files that export the same package and you want access to both packages classes you use a mechanism called "shading". Shading is when you take those two packages and you gather their contents together in another .jar files package. This used to be done by a maven plugin called "maven-shade-plugin" but now the functionality is part of the maven-bundle plugin.

First, create a new project, we'll call ours "badlyPlannedImplementationShaded". Then, in your project create a pom.xml file. In your dependency section, include dependencies for both of your .jar files that you're trying to shade together.

Then, add the following to your build section.

<plugin>
   <groupId>org.apache.felix</groupId>
   <artifactId>maven-bundle-plugin</artifactid>
   <version>2.1.0</version>
   <extensions>true</extensions>
   <configuration>
      <instructions>
         <Bundle-Version>${project.version}</Bundle-Version>
         <Export-Package>
            badlyPlannedImplementation.util;version="1",
            badlyPlannedImplementation.util;version="2"
         </Export-Package>
      </instructions>
   </configuration>
</plugin> 

Doing this will create a new bundle that contains a util package that has all of the classes from the two .jar files you were trying to use.

I hope that helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文