OSGI 包依赖

OSGI bundle dependencies

我创建了一个依赖于 SLF4J 的包,因此我将 Logback 用于 OSGI 实现。这一切都捆绑在一起并安装正常但是当我开始捆绑时我得到以下异常:

org.osgi.framework.BundleException: Unable to resolve com.felix.test [20](R 20.0): missing requirement [com.felix.test [20](R 20.0)] osgi.wiring.package; (osgi.wiring.package=groovy.lang) Unresolved requirements: [[com.felix.test [20](R 20.0)] osgi.wiring.package; (osgi.wiring.package=groovy.lang)]

我可以在我的清单文件中看到 groovy.lang 列在 Import-Package 中,我很确定问题是 Logback 正在被嵌入,但它的引用 none 是。

我正在使用它来创建包,这是配置:

<plugin>
    <groupId>org.apache.felix</groupId>
    <artifactId>maven-bundle-plugin</artifactId>
    <version>2.5.4</version>
    <extensions>true</extensions>
    <configuration>
        <instructions>
            <Import-Package>
                *
            </Import-Package>
            <Embed-Dependency>
                *
            </Embed-Dependency>
            <Embed-Directory>
                osgi-inf/libs
            </Embed-Directory>
            <Embed-Transitive>
                true
            </Embed-Transitive>
        </instructions>
    </configuration>
</plugin>

这是我的清单:

Manifest-Version: 1.0
Bnd-LastModified: 1436982944102
Build-Jdk: 1.8.0_45
Built-By: tim.clifford
Bundle-ClassPath: .,osgi-inf/libs/org.osgi.core-1.0.0.jar,osgi-inf/libs/
 servlet-api-2.5.jar,osgi-inf/libs/org.apache.felix.scr.annotations-1.9.
 6.jar,osgi-inf/libs/httpclient-osgi-4.5.jar,osgi-inf/libs/httpclient-4.
 5.jar,osgi-inf/libs/httpcore-4.4.1.jar,osgi-inf/libs/commons-logging-1.
 2.jar,osgi-inf/libs/commons-codec-1.9.jar,osgi-inf/libs/httpmime-4.5.ja
 r,osgi-inf/libs/httpclient-cache-4.5.jar,osgi-inf/libs/fluent-hc-4.5.ja
 r,osgi-inf/libs/ehcache-2.10.0.jar,osgi-inf/libs/slf4j-api-1.7.7.jar,os
 gi-inf/libs/commons-lang3-3.4.jar,osgi-inf/libs/logback-classic-1.1.3.j
 ar,osgi-inf/libs/logback-core-1.1.3.jar
Bundle-ManifestVersion: 2
Bundle-Name: com.felix.test
Bundle-SymbolicName: com.felix.test
Bundle-Version: 1.0.0.SNAPSHOT
Created-By: Apache Maven Bundle Plugin
Embed-Dependency: *
Embed-Directory: osgi-inf/libs
Embed-Transitive: true
Embedded-Artifacts: osgi-inf/libs/org.osgi.core-1.0.0.jar;g="org.apache.
 felix";a="org.osgi.core";v="1.0.0",osgi-inf/libs/servlet-api-2.5.jar;g=
 "javax.servlet";a="servlet-api";v="2.5",osgi-inf/libs/org.apache.felix.
 scr.annotations-1.9.6.jar;g="org.apache.felix";a="org.apache.felix.scr.
 annotations";v="1.9.6",osgi-inf/libs/httpclient-osgi-4.5.jar;g="org.apa
 che.httpcomponents";a="httpclient-osgi";v="4.5",osgi-inf/libs/httpclien
 t-4.5.jar;g="org.apache.httpcomponents";a="httpclient";v="4.5",osgi-inf
 /libs/httpcore-4.4.1.jar;g="org.apache.httpcomponents";a="httpcore";v="
 4.4.1",osgi-inf/libs/commons-logging-1.2.jar;g="commons-logging";a="com
 mons-logging";v="1.2",osgi-inf/libs/commons-codec-1.9.jar;g="commons-co
 dec";a="commons-codec";v="1.9",osgi-inf/libs/httpmime-4.5.jar;g="org.ap
 ache.httpcomponents";a="httpmime";v="4.5",osgi-inf/libs/httpclient-cach
 e-4.5.jar;g="org.apache.httpcomponents";a="httpclient-cache";v="4.5",os
 gi-inf/libs/fluent-hc-4.5.jar;g="org.apache.httpcomponents";a="fluent-h
 c";v="4.5",osgi-inf/libs/ehcache-2.10.0.jar;g="net.sf.ehcache";a="ehcac
 he";v="2.10.0",osgi-inf/libs/slf4j-api-1.7.7.jar;g="org.slf4j";a="slf4j
 -api";v="1.7.7",osgi-inf/libs/commons-lang3-3.4.jar;g="org.apache.commo
 ns";a="commons-lang3";v="3.4",osgi-inf/libs/logback-classic-1.1.3.jar;g
 ="ch.qos.logback";a="logback-classic";v="1.1.3",osgi-inf/libs/logback-c
 ore-1.1.3.jar;g="ch.qos.logback";a="logback-core";v="1.1.3"
Export-Package: com.felix.test;version="1.0.0"
Import-Package: groovy.lang,javax.crypto,javax.crypto.spec,javax.jms,jav
 ax.mail,javax.mail.internet,javax.management,javax.management.openmbean
 ,javax.naming,javax.naming.directory,javax.naming.ldap,javax.net,javax.
 net.ssl,javax.security.auth.x500,javax.sql,javax.swing.event,javax.tran
 saction,javax.transaction.xa,javax.xml.datatype,javax.xml.namespace,jav
 ax.xml.parsers,javax.xml.stream,javax.xml.stream.events,net.spy.memcach
 ed,org.apache.avalon.framework.logger,org.apache.felix.scrplugin,org.ap
 ache.felix.scrplugin.annotations,org.apache.felix.scrplugin.description
 ,org.apache.log,org.apache.log4j,org.codehaus.commons.compiler,org.code
 haus.groovy.control,org.codehaus.groovy.control.customizers,org.codehau
 s.groovy.reflection,org.codehaus.groovy.runtime,org.codehaus.groovy.run
 time.callsite,org.codehaus.groovy.runtime.typehandling,org.codehaus.gro
 ovy.runtime.wrappers,org.codehaus.groovy.transform,org.codehaus.janino,
 org.hibernate,org.hibernate.cache,org.hibernate.cache.access,org.hibern
 ate.cfg,org.hibernate.impl,org.hibernate.stat,org.hibernate.transaction
 ,org.ietf.jgss,org.osgi.service.cm,org.quartz,org.quartz.impl,org.quart
 z.impl.jdbcjobstore,org.quartz.impl.matchers,org.quartz.simpl,org.terra
 cotta.quartz,org.terracotta.toolkit,org.terracotta.toolkit.atomic,org.t
 erracotta.toolkit.builder,org.terracotta.toolkit.cache,org.terracotta.t
 oolkit.cluster,org.terracotta.toolkit.collections,org.terracotta.toolki
 t.concurrent.locks,org.terracotta.toolkit.config,org.terracotta.toolkit
 .events,org.terracotta.toolkit.feature,org.terracotta.toolkit.internal,
 org.terracotta.toolkit.internal.cache,org.terracotta.toolkit.internal.c
 luster,org.terracotta.toolkit.internal.collections,org.terracotta.toolk
 it.internal.concurrent.locks,org.terracotta.toolkit.internal.feature,or
 g.terracotta.toolkit.monitoring,org.terracotta.toolkit.nonstop,org.terr
 acotta.toolkit.rejoin,org.terracotta.toolkit.search,org.terracotta.tool
 kit.search.attribute,org.terracotta.toolkit.store,org.xml.sax,org.xml.s
 ax.helpers,sun.misc,sun.reflect
Require-Capability: osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=1.6))"
Tool: Bnd-2.4.1.201501161923

我的做法是否正确或是否有更好的方法?

谢谢!

您不应将所有依赖项嵌入到您的包中。而只是 运行 与 maven bundle 插件默认值。它应该创建一个包,然后您可以将其部署到 OSGi 容器中。

我对 Apache Karaf 最有经验。在那里你可以使用安装包:

install -s mvn:groupId/artifactId/version

在您的情况下,这可能已经有效,因为 Apache Karaf 预装了 pax-logging。

如果你想使用普通的 felix,它会稍微复杂一些,因为你必须收集所有依赖项并创建合适的启动配置。在那种情况下 bndtools 可能会有所帮助。

根据您在问题中报告的异常,我会说您的 osgi 环境中的 none 包当前正在导出 groovy.lang 包。 我建议在您的运行时安装 felix gogo shell 并在 osgi 控制台中发出以下命令:

g! inspect cap osgi.wiring.package

这将为您提供 osgi 实例中所有导出包的列表:通过这种方式,您可以验证 groovy.lang 包是否确实丢失。

如果是这种情况,那么您应该包装 groovy-all jar in a bundle as explained here and include it in your running osgi bundles. If you want, you can search a ready-to-use osgi bundle for groovy-all in jpm4j 网站。

你的问题"am I going about is the right way",答案是"definitely not"!

拜托,请不要使用Embed-Dependency。这只是获取所有传递依赖图(在您的情况下,它以某种方式包括 Groovy 语言运行时)并将其粘贴到您的包中。

这完全没有抓住 OSGi 的重点。更糟糕的是,当您开发下一个包时,您将不得不再次经历所有这些!最终您将拥有数十个捆绑包,每个捆绑包都带有大量的嵌入式依赖项。

正如 Christian 所说,坚持 maven-bundle-plugin 提供的默认值。您甚至不需要指定 <Import-Package>*</Import-Package>,因为这已经是默认值。因此,您将获得一个包含包导入的包,这是一件好事!但是您需要安装提供与您的导入相匹配的导出的包。