如何在 OSGi 容器中的捆绑包之间共享非 OSGi 库?

发布于 2024-07-30 10:24:59 字数 329 浏览 2 评论 0原文

尝试共享 Struts 包时,我遇到了这个问题OSGi 容器内的多个包之间。 我想避免在包内重复依赖项并在它们之间引入新的依赖项(通过让一个包导出其内部依赖项以供另一个包共享)。

事实证明,如果您碰巧使用 Maven,那么答案非常简单,如果您没有使用 Maven,您仍然可以使用 Maven 解决方案来学习如何通过其他机制创建相同的结果。

我发现这种方法在我的项目中多次有用,所以我将在这里发布一个示例。

I came across this question when trying to share the Struts packages among multiple bundles inside an OSGi container. I wanted to avoid both duplicating dependencies inside the bundles and introducing a new dependency between them (by having one bundle export its internal dependencies for the other to share).

It turns out the answer is very easy if you happen to use Maven, and if you aren't, you can still use the Maven solution to learn how to create the same result with some other mechanism.

I found this approach useful multiple times during my project, so I'll post an example here.

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

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

发布评论

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

评论(2

浪菊怪哟 2024-08-06 10:24:59

对于那些不太热衷于 Maven、无法移植或对 ant/ivy 非常满意的人来说,还有一条额外的途径,

我发现完成所述任务的最简单方法是通过让清单导出每个包并添加一些适当的符号名称/版本。 我已经能够通过 ant 任务(甚至直接命令行)使用 bnd 非常轻松地完成此操作调用)。 还有存储库,其中包含许多流行库的“osgi-ified”版本。 一些库(joda-time)已经附带了正确的 OSGi 清单。

An additional path for those not so keen on maven, unable to port, or perfectly happy with ant/ivy

I've found the that easiest way to accomplish the stated task is to turn the non-OSGi library into an OSGi library by letting the manifest export every package and add on some approriate symbolic names / versions. I've been able to do this VERY easily with bnd via ant tasks (or even direct command line invocation). There are also repositories which contain "osgi-ified" version of many popular libraries. Some libraries (joda-time) are already shipping with correct OSGi manifests.

不寐倦长更 2024-08-06 10:24:59

使用 Maven,可以非常轻松地从任何库创建 OSGi 包。 然而,我认为使用其他机制也可以产生相同的结果。 Maven 解决方案帮助我了解它是如何工作的。

创建捆绑包是通过创建一个以该库作为依赖项的项目,然后使用 maven-bundle-plugin 来自 Apache Felix项目并使用Export-Package指令指定库包。 我用它在 OSGi 容器内的捆绑包之间共享 Google Protocol Buffers:

<?xml version="1.0" encoding="UTF-8" ?>
<project>
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.example.lib</groupId>
  <artifactId>protobuf-dist</artifactId>
  <version>2.1.0</version>
  <name>Google Protocol Buffers OSGi Distribution</name>
  <packaging>bundle</packaging>

  <dependencies>
    <dependency>
      <groupId>com.google.protobuf</groupId>
      <artifactId>protobuf-java</artifactId>
      <version>2.1.0</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.felix</groupId>
        <artifactId>maven-bundle-plugin</artifactId>
        <extensions>true</extensions>
        <configuration>
          <instructions>
            <Export-Package>com.google.protobuf</Export-Package>
          </instructions>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

如果您也希望将所有传递依赖项都汇总到捆绑包中,请使用插件的 bundleall 目标。

该插件识别并尊重依赖项中现有的 OSGi 清单。

您还可以使用捆绑插件来创建清单,并告诉 jar 打包插件(或 jar-with-dependencies 内置程序集)通过存档使用该清单部分。 上面链接的插件页面显示了如何做到这一点。

Using Maven, it is very easy to create an OSGi bundle from any library. However, I think the same result can be created with other mechanisms, too. The Maven solution helped me understand how it works.

Creating the bundle is done by creating a project which has the library as a dependency and then packaging the project using the maven-bundle-plugin from the Apache Felix project and specifying the library packages with the Export-Package instruction. I used this to share Google Protocol Buffers between bundles inside an OSGi container:

<?xml version="1.0" encoding="UTF-8" ?>
<project>
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.example.lib</groupId>
  <artifactId>protobuf-dist</artifactId>
  <version>2.1.0</version>
  <name>Google Protocol Buffers OSGi Distribution</name>
  <packaging>bundle</packaging>

  <dependencies>
    <dependency>
      <groupId>com.google.protobuf</groupId>
      <artifactId>protobuf-java</artifactId>
      <version>2.1.0</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.felix</groupId>
        <artifactId>maven-bundle-plugin</artifactId>
        <extensions>true</extensions>
        <configuration>
          <instructions>
            <Export-Package>com.google.protobuf</Export-Package>
          </instructions>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

If you want all transitive dependencies rolled into the bundle, too, use the bundleall goal of the plugin.

The plugin recognizes and honours existing OSGi manifests in the dependency.

You can also use the bundle plugin to just create the manifest and tell the jar packaging plugin (or the jar-with-dependencies builtin assembly) to use that manifest via the archive section. The plugin's page linked above shows how to do that.

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