Karaf / Maven - 无法解决:缺少要求 osgi.wiring.package

38

我无法在Karaf(版本3.0.1)中启动一个bundle。

该bundle是使用maven构建的,并且导入了gson

我按要求在maven中包含了gson:

<dependency>
     <groupId>com.google.code.gson</groupId>
     <artifactId>gson</artifactId>
     <version>2.3.1</version>
</dependency>

编译过程顺利完成。然而,在部署捆绑包时,我收到了 Karaf 发出的警告:

2015-05-27 12:45:07,371 | WARN  | 49-19-bin/deploy | fileinstall                      | 11 - org.apache.felix.fileinstall - 3.2.8 | Error while starting bundle: file:/Users/user/Documents/tools/MyBundle-1.0.0-SNAPSHOT.jar
org.osgi.framework.BundleException: Unresolved constraint in bundle MyBundle [121]: Unable to resolve 121.13: missing requirement [121.13] osgi.wiring.package; (&(osgi.wiring.package=com.google.gson)(version>=2.3.0)(!(version>=3.0.0)))
at org.apache.felix.framework.Felix.resolveBundleRevision(Felix.java:3974)[org.apache.felix.framework-4.2.1.jar:]
at org.apache.felix.framework.Felix.startBundle(Felix.java:2037)[org.apache.felix.framework-4.2.1.jar:]
at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:955)[org.apache.felix.framework-4.2.1.jar:]
at org.apache.felix.fileinstall.internal.DirectoryWatcher.startBundle(DirectoryWatcher.java:1263)[11:org.apache.felix.fileinstall:3.2.8]
at org.apache.felix.fileinstall.internal.DirectoryWatcher.startBundles(DirectoryWatcher.java:1235)[11:org.apache.felix.fileinstall:3.2.8]
at org.apache.felix.fileinstall.internal.DirectoryWatcher.startAllBundles(DirectoryWatcher.java:1224)[11:org.apache.felix.fileinstall:3.2.8]
at org.apache.felix.fileinstall.internal.DirectoryWatcher.process(DirectoryWatcher.java:519)[11:org.apache.felix.fileinstall:3.2.8]
at org.apache.felix.fileinstall.internal.DirectoryWatcher.run(DirectoryWatcher.java:308)[11:org.apache.felix.fileinstall:3.2.8]
捆绑包未安装。

MANIFEST.MF中的Import-Package以以下内容开头:
Import-Package: com.google.gson;version="[2.3,3)"

我尝试使用以下方式进行更改:

Import-Package: com.google.code.gson;version="[2.3,3)"

但是它给了我一个类似的错误:

missing requirement [121.13] osgi.wiring.package; (&(osgi.wiring.package=com.google.code.gson)

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>

<parent>
    <groupId>groupid</groupId>
    <artifactId>artifactid</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>

<groupId>groupid</groupId>
<artifactId>MyBundle</artifactId>
<version>1.0.0-SNAPSHOT</version>

<packaging>bundle</packaging>

<name>MyBundle</name>
<description>MyBundle</description>

<properties>
    <skipTests>true</skipTests>
</properties>

<dependencies>
    ...
    <dependency>
        <groupId>org.apache.felix</groupId>
        <artifactId>org.apache.felix.http.jetty</artifactId>
    </dependency>
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>com.springsource.org.json</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.servicemix.bundles</groupId>
        <artifactId>org.apache.servicemix.bundles.commons-httpclient</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.4-1201-jdbc41</version>
    </dependency>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.3.1</version>
    </dependency>
    <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>1.10</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <extensions>true</extensions>
            <configuration>
                <instructions>
                    <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
                </instructions>
            </configuration>
        </plugin>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                  <skipTests>${skipTests}</skipTests>
            </configuration>
          </plugin>
    </plugins>
</build>

</project>

当我构建捆绑包时,Maven下载的Google GSON的MANIFEST.MF版本为:

Manifest-Version: 1.0
Export-Package: com.google.gson;version=2.3.1, com.google.gson.annotat
 ions;version=2.3.1, com.google.gson.reflect;version=2.3.1, com.google
 .gson.stream;version=2.3.1, com.google.gson.internal;version=2.3.1, c
 om.google.gson.internal.bind;version=2.3.1
Bundle-ClassPath: .
Built-By: inder
Bundle-Name: Gson
Created-By: Apache Maven 3.2.1
Bundle-RequiredExecutionEnvironment: J2SE-1.5, JavaSE-1.6, JavaSE-1.7,
  JavaSE-1.8
Bundle-Vendor: Google Gson Project
Bundle-ContactAddress: http://code.google.com/p/google-gson/
Build-Jdk: 1.6.0_65
Bundle-Version: 2.3.1
Bundle-ManifestVersion: 2
Bundle-Description: Google Gson library
Bundle-SymbolicName: com.google.gson
Archiver-Version: Plexus Archiver

我也尝试了

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <extensions>true</extensions>
            <configuration>
                <instructions>
                    <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
                    <Import-Package>*;resolution:=optional</Import-Package>
                    <Embed-Dependency>*</Embed-Dependency>
                    <Embed-Transitive>true</Embed-Transitive>
                </instructions>
            </configuration>
        </plugin>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <skipTests>${skipTests}</skipTests>
            </configuration>
          </plugin>
    </plugins>
</build>

现在依赖项已经嵌入,但是Karaf无法启动该捆绑包。


你是否意识到 <groupId>com.google.code.gson</groupId>osgi.wiring.package=com.google.gson 之间的包不匹配? - javabrett
是的,谢谢。我搜索了com.google.gson包,但据我所知只有com.google.code.gson。然而,我不知道所需的包为什么是com.google.gson。 有任何线索吗? - gc5
看起来应该没问题。你能否发布你的POM中的捆绑配置,Import-Package等内容? - javabrett
已更新,我已隐藏了一些专有信息,但应该没问题。如果有问题,请告诉我。 - gc5
4个回答

57

我相信你在这里有两个选择。

如果你的MANIFEST.MF中有Import-Package: com.google.gson;version="[2.3,3)",这意味着你想要从已部署的 bundle 中导入一些包,而不是从一个内嵌的 jar 文件中。在这种情况下,你应该先部署 gson-2.3.1.jar bundle(将此文件复制到 deploy 文件夹中),然后再部署你的 bundle。

如果你想将 gson 库作为简单的非 OSGi jar 依赖项嵌入,也可以实现,但是你必须从Import-Package中排除其包:

    <plugin>
        <groupId>org.apache.felix</groupId>
        <artifactId>maven-bundle-plugin</artifactId>
        <extensions>true</extensions>
        <configuration>
            <instructions>
                <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
                <Import-Package>!com.google.gson,*</Import-Package>
                <Embed-Dependency>gson</Embed-Dependency>
            </instructions>
        </configuration>
    </plugin>

Embed-Dependecy解决了一个类似的问题,解决了org.apache.derby.jdbc的依赖。谢谢! - Cesar
6
我认为<Import-Package>标签应该进行一些更改,我已经亲自测试过了。<Import-Package>!com.google.gson,*</Import-Package>,否则com.google.gson将不会被排除。 - Dave Pateral

0

我遇到了同样的问题:

org.osgi.service.resolver.ResolutionException: 无法解析根:缺少需要 [root] osgi.identity; ...... [造成原因:无法解析 com.mycompany.somemodule/1.0.0.SNAPSHOT: 缺少需要]

而且通过更改有问题的捆绑包(例如 com.mycompany.somemodule/pom.xml)的pom.xml文件来解决了该问题:

            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <configuration>
                <instructions>
                    <Export-Package>
                        com.mycompany.module.entity,
                        com.mycompany.somemodule.entity.v1,
                        com.mycompany.somemodule.entity.v2,
                        com.mycompany.somemodule.util
                    </Export-Package>
                    <Private-Package>
                        com.mycompany.somemodule,
                        com.mycompany.somemodule.translator
                    </Private-Package>
                    <Bundle-Name>Some Module</Bundle-Name>
                    <Bundle-SymbolicName>${project.name}</Bundle-SymbolicName>
                    <Bundle-Version>${project.version}</Bundle-Version>
                </instructions>

最初的问题是我只暴露了这些包的子集:

               ...
               <Export-Package>
                    com.mycompany.somemodule.entity.v2
                </Export-Package>
                <Private-Package>
                </Private-Package>
                ...

0
我遇到了这种错误。 这意味着Karaf没有找到“依赖包”。 先安装依赖包,然后再安装您的包。
例如:
osgi:install mvn:com.yourCompany.corePackage.subPackage/yourDependencyBundle/1.0.0-SNAPSHOT

那么

osgi:install mvn:com.yourCompany.corePackage.subPackage/yourBundle/1.0.0-SNAPSHOT

0

我也遇到了同样的问题:

"无法解析 blah-blah:缺少要求 blah-blah osgi.wiring.package; (osgi.wiring.package=blah-blah) 未解决的要求:[blah-blah] osgi.wiring.package; (osgi.wiring.package=blah-blah)]。

我的激活器类位于 src/main/java 内。当我在 Java 中创建了一个新的包并将我的激活器放在这里时,问题得到了解决。结果是 src/main/java/oneMorePackage/MyActivator

标签看起来像这样:

<Bundle-Activator>
       oneMorePackage.MyActivator
</Bundle-Activator>

<Private-Package>
      oneMorePackage
</Private-Package>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接