java 如何使用 maven 包插件在 OSGi 包中包含一个依赖包?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16605746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:26:56  来源:igfitidea点击:

How to include a jar of dependency in an OSGi bundle using maven bundle plugin?

javamavenosgi-bundlemaven-bundle-plugin

提问by Rahul Bobhate

I have an OSGi compliant bundle(jar), in which I want to add a jar of a dependency. The dependecy I want to add is of a Database Driver. That jar is not present in the lib folder of the Karaf container which I am using, and there is no way of adding it there manually. I have access only to the deploy folder, where I can deploy my bundles. I am using maven bundle plugin to package my bundle. So, I wanted to know whether there is a way to add the dependency jar in my bundle. Currently, I am adding the jar manually to the bundle by opening the bundle in 7zip and adding the jar by copying it in the jar and it works fine. I tried using the <embed-dependency>tag, but after doing that the bundle doesn't gets deployed. Is there anyway to do it ?

我有一个符合 OSGi 的 bundle(jar),我想在其中添加一个依赖项的 jar。我要添加的依赖项是数据库驱动程序。我正在使用的 Karaf 容器的 lib 文件夹中不存在该 jar,并且无法手动将其添加到那里。我只能访问 deploy 文件夹,在那里我可以部署我的包。我正在使用 maven bundle 插件来打包我的包。所以,我想知道是否有办法在我的包中添加依赖 jar。目前,我通过在 7zip 中打开包并通过将其复制到 jar 中来添加 jar 来手动将 jar 添加到包中,并且它工作正常。我尝试使用该<embed-dependency>标签,但在使用该标签后并未部署该包。有什么办法吗?

The following is the dependency in pom.xmlwhich I want to add in the bundle:

以下是pom.xml我要添加到包中的依赖项:

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.3.158</version>
    </dependency>

The following is the build tag in pom.xml:

以下是 中的构建标记pom.xml

<build>
    <defaultGoal>install</defaultGoal>
    <plugins>
        <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <extensions>true</extensions>
            <configuration>
                <instructions>
                    <Export-Package>
                        com.ct.service.userService.*,
                        org.h2.*
                    </Export-Package>
                    <Import-Package>
                        *,
                        org.codehaus.Hymanson.jaxrs
                    </Import-Package>
                    <Embed-Dependency>h2</Embed-Dependency>
                </instructions>
            </configuration>
        </plugin>
    </plugins>
</build>

I get the following error when I try to deploy it:

尝试部署时出现以下错误:

ERROR: Bundle com.ge.dsp.userService [205] Error starting file:D:Karaf/deploy/userService-0.0.1-SNAPSHOT.jar (org.osgi.framework.BundleException: Unresolved constraint in bundle com.ge.dsp.userService [205]: Unable to resolve 205.2: missing requirement [205.2] osgi.wiring.package; (osgi.wiring.package=org.apache.lucene.analysis))

org.osgi.framework.BundleException: Unresolved constraint in bundle com.ct.service.userService [205]: Unable to resolve 205.2: missing requirement [205.2] osgi.wiring.package; (osgi.wiring.package=org.apache.lucene.analysis)
    at org.apache.felix.framework.Felix.resolveBundleRevision(Felix.java:3826)
    at org.apache.felix.framework.Felix.startBundle(Felix.java:1868)
    at org.apache.felix.framework.Felix.setActiveStartLevel(Felix.java:1191)
    at org.apache.felix.framework.FrameworkStartLevelImpl.run(FrameworkStartLevelImpl.java:295)
    at java.lang.Thread.run(Thread.java:662)

采纳答案by Rahul Bobhate

It seems like I needed to deploy the h2-1.3.158.jaralong with my bundle and add the make some edits in the pom.xmlas follows:

似乎我需要将它h2-1.3.158.jar与我的包一起部署并添加如下进行一些编辑pom.xml

<build>
<defaultGoal>install</defaultGoal>
<plugins>
    <plugin>
        <groupId>org.apache.felix</groupId>
        <artifactId>maven-bundle-plugin</artifactId>
        <extensions>true</extensions>
        <configuration>
            <instructions>
                <Export-Package>
                    com.ct.service.userService.*,
                    <!--org.h2.*    No need to export these dependency -->
                </Export-Package>
                <Import-Package>
                    *,
                    org.codehaus.Hymanson.jaxrs,
                    org.h2               <!-- Needed to import the dependencies. -->
                </Import-Package>
                <!--<Embed-Dependency>h2</Embed-Dependency> No need of embedding -->
            </instructions>
        </configuration>
    </plugin>
</plugins>