如何在OSGI包中嵌入外部jar依赖项? [英] How to embed external jars dependency in OSGI bundles?

查看:135
本文介绍了如何在OSGI包中嵌入外部jar依赖项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的项目转换为OSGI应用程序.我毫不怀疑.假设我的应用程序中的ModuleA依赖于外部jars jarA和jarB.现在运行ModeuleA,我使用maven-bundle-plugin的embed-dependency属性嵌入两个jar. 现在假设我还有另一个模块ModuleB,它也依赖于jarA.因此,此模块还嵌入了jarA.我的项目最终将jarA嵌入了2次,这将不必要地膨胀项目的大小.

有没有办法告诉OSGI只加载jarA一次并将其提供给两个模块.

如果将这些罐子转换为OSGI捆绑包是唯一的解决方案,那么我还有其他问题:

  1. 将jar转换为包的最简单方法是什么. BND工具看起来是一个很好的解决方案,但是我找不到关于它的适当文档.

  2. jarA也将有一些依赖的jar.因此,我是否还需要将所有相关的jar都转换为bundle.我的项目有100多个罐子.如何使该过程自动化.

先谢谢您了:)

解决方案

实际上有一些解决方案,两者都与您现在正在做的事情有些不同:

  1. 构建一个 one 第三方依赖项"捆绑包,该捆绑包将嵌入您的项目具有的所有非OSGi依赖项.
  2. 将每个非OSGi依赖项转换为OSGi捆绑包.

选项1更易于处理,因此我认为大多数项目都可以做到这一点.我个人更喜欢选项2.我们有一个Maven"pom.xml"模板,可用来转换那些依赖关系.

"pom.xml"如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <properties>
        <library.groupId></library.groupId>
        <library.artifactId></library.artifactId>
        <library.version></library.version>
    </properties>

    <artifactId></artifactId>
    <packaging>bundle</packaging>

    <name></name>
    <description>${library.groupId}:${library.artifactId}:${library.version}</description>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <Import-Package>*;resolution:=optional</Import-Package>
                        <Export-Package>*</Export-Package>
                        <Embed-Dependency>*;scope=compile|runtime;inline=true</Embed-Dependency>
                        <Embed-Transitive>true</Embed-Transitive>
                    </instructions>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>${library.groupId}</groupId>
            <artifactId>${library.artifactId}</artifactId>
            <version>${library.version}</version>
        </dependency>
    </dependencies>

</project>

这样做:

  1. 将非OSGi库添加为依赖项
  2. 告诉maven-bundle-plugin嵌入此依赖项(可传递)
  3. 告诉maven-bundle-plugin导出所有依赖项包

我把一些必须设置的空白留了下来,例如library.groupIdlibrary.artifactIdlibrary.version.还有一些我们需要调整maven-bundle-plugin的配置.但这是我们的出发点.例如,您不想导出所有软件包等.

如果确实有100多个需要转换的依赖项,则最好使用此模板,然后将所有100个依赖项都添加为依赖项,并在其中包含所有这些依赖项来构建一个大捆绑包.

您可以在这里找到maven-bundle-plugin的文档:

https://felix.apache .org/documentation/subprojects/apache-felix-maven-bundle-plugin-bnd.html

在这一点上,我还想提到一个新的捆绑插件,您可能需要考虑使用它:bnd-maven-plugin.

请参阅: https://github.com/bndtools /bnd/tree/master/maven/bnd-maven-plugin

I am trying to convert my project into an OSGI application. I have few doubts. Suppose ModuleA in my application is dependent on external jars jarA and jarB. Now to make ModeuleA run, I am embedding both the jars using embed-dependency property of maven-bundle-plugin. Now suppose I have another module ModuleB which is also dependent on jarA. So this module also embeds the jarA. My project ends up having jarA being embedded 2 times which will unnecessarily bloat the size of project.

Is there any way to tell OSGI to load jarA only once and provide it to both the modules.

If converting these jars to OSGI bundles is the only solution, I have few more questions:

  1. What is the easiest way to convert jar to a bundle. BND tool looks like a good solution but I am not able to find proper documentation about it.

  2. jarA will also have some dependent jars. So do I need to convert all the dependent jars to bundles also. My project has more than 100 jars. How can I automate this process.

Thanks in advance :)

解决方案

There are actually to solutions to this, both a little bit different from what you are doing right now:

  1. Build one "third party dependencies" bundle, which will embed all of the non OSGi dependencies your project has.
  2. Convert every non OSGi dependency to a OSGi bundle.

Option 1 is easier to handle so I think most projects do this. I, personally, prefer option 2. We have a Maven "pom.xml" template that we use to convert those dependencies.

The "pom.xml" looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <properties>
        <library.groupId></library.groupId>
        <library.artifactId></library.artifactId>
        <library.version></library.version>
    </properties>

    <artifactId></artifactId>
    <packaging>bundle</packaging>

    <name></name>
    <description>${library.groupId}:${library.artifactId}:${library.version}</description>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <Import-Package>*;resolution:=optional</Import-Package>
                        <Export-Package>*</Export-Package>
                        <Embed-Dependency>*;scope=compile|runtime;inline=true</Embed-Dependency>
                        <Embed-Transitive>true</Embed-Transitive>
                    </instructions>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>${library.groupId}</groupId>
            <artifactId>${library.artifactId}</artifactId>
            <version>${library.version}</version>
        </dependency>
    </dependencies>

</project>

This does:

  1. Add the non OSGi library as dependency
  2. Tell maven-bundle-plugin to embed this dependency (transitive)
  3. Tell maven-bundle-plugin to export all of the dependencies packages

I left some things blank that you have to set like library.groupId, library.artifactId and library.version. And somethings we need to tweak the configuration of the maven-bundle-plugin. But this is our starting point. Somethings for example, you do not want to export all packages etc.

If you really have 100+ dependencies that you need to convert you might be better of using this template and just adding all of your 100 dependencies as dependency and build one big bundle with all of them inside.

You can find the documentation for the maven-bundle-plugin here:

https://felix.apache.org/documentation/subprojects/apache-felix-maven-bundle-plugin-bnd.html

At this point I also want to mention that there is a new bundle plugin that you might want to consider for this: bnd-maven-plugin.

See: https://github.com/bndtools/bnd/tree/master/maven/bnd-maven-plugin

这篇关于如何在OSGI包中嵌入外部jar依赖项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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