在 Karaf 中启动捆绑包时出现 OSGi os.wiring.package 错误

OSGi os.wiring.package error when starting bundle in Karaf

我正在深入研究 OSGi 领域并构建了一个我试图安装到 Karaf 中的包。我收到以下错误:

我想知道这是否是蓝图的工作方式,或者我是否以某种方式不正确 exporting/importing 模块?

我正在使用蓝图,我的 impl 蓝图如下所示:

实现的蓝图文件

<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
           xmlns:cxf="http://cxf.apache.org/blueprint/core"
           xmlns:jaxrs="http://cxf.apache.org/schemas/jaxrs.xsd"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://cxf.apache.org/blueprint/core ">

    <cxf:bus id="greetingServiceBus">
        <cxf:features>
            <cxf:logging/>
        </cxf:features>
    </cxf:bus>

    <bean id="welcomeImpl" class="welcomeimpl.WelcomeImpl"/>
    <service ref="welcomeImpl" interface="welcome.WelcomeAPI"/>

    <jaxrs:server address="/tester" id="tester">
        <jaxrs:serviceBeans>
            <ref component-id="welcomeImpl"/>
        </jaxrs:serviceBeans>
    </jaxrs:server>
</blueprint>

错误

Error executing command: Error executing command on bundles: Error starting bundle 119: Unable to resolve impl [119](R 119.0): missing requirement [impl [119](R 119.0)] osgi.wiring.package; (osgi.wiring.package=welcome) Unresolved requirements: [[impl [119](R 119.0)] osgi.wiring.package; (osgi.wiring.package=welcome)]

实施POM.xml

   <parent>
        <artifactId>learning-osgi</artifactId>
        <groupId>com.osgirest</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <packaging>bundle</packaging>

    <artifactId>impl</artifactId>
    <dependencies>
        <dependency>
            <groupId>com.osgirest</groupId>
            <artifactId>welcome-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>javax.ws.rs-api</artifactId>
            <version>2.0.1</version>
            <scope>provided</scope>
        </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>
                        <Bundle-Version>${project.version}</Bundle-Version>
                        <Export-Package>
                            javax.ws.rs*,
                            com.learning.welcomeimpl
                        </Export-Package>
                        <Import-Package>
                            *,
                            javax.ws.rs*
                        </Import-Package>
                    </instructions>
                </configuration>
            </plugin>
        </plugins>
    </build>

api POM.xml

<parent>
    <artifactId>learning-osgi</artifactId>
    <groupId>com.osgirest</groupId>
    <version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>welcome-api</artifactId>
<packaging>bundle</packaging>

<name>welcome-api Bundle</name>
<description>api to welcome people into the application</description>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <configuration>
                <instructions>
                    <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
                    <Bundle-Version>${project.version}</Bundle-Version>
                    <Bundle-Activator/>
                    <Export-Package>
                        com.learning.welcome;version=${project.version}
                    </Export-Package>
                    <Import-Package>
                        *
                    </Import-Package>
                </instructions>
            </configuration>
        </plugin>
    </plugins>
</build>

目录结构

├── impl
│   ├── impl.iml
│   ├── pom.xml
│   ├── src
│   │   ├── main
│   │   │   ├── java
│   │   │   │   └── com
│   │   │   │       └── learning
│   │   │   │           └── welcomeimpl
│   │   │   │               └── WelcomeImpl.java
│   │   │   └── resources
│   │   │       └── OSGI-INF
│   │   │           └── blueprint
│   │   │               └── blueprint.xml
│   │   └── test
│   │       └── java
│   └── target
│       ├── classes
│       │   ├── META-INF
│       │   │   └── MANIFEST.MF
│       │   ├── OSGI-INF
│       │   │   └── blueprint
│       │   │       └── blueprint.xml
│       │   └── com
│       │       └── learning
│       │           └── welcomeimpl
│       │               └── WelcomeImpl.class
│       ├── generated-sources
│       │   └── annotations
│       ├── impl-1.0-SNAPSHOT.jar
│       └── maven-status
│           └── maven-compiler-plugin
│               ├── compile
│               │   └── default-compile
│               │       ├── createdFiles.lst
│               │       └── inputFiles.lst
│               └── testCompile
│                   └── default-testCompile
│                       └── inputFiles.lst
├── learningosgi.iml
├── pom.xml
├── src
│   ├── main
│   │   ├── java
│   │   └── resources
│   └── test
│       └── java
└── welcomeapi
    ├── pom.xml
    ├── src
    │   ├── main
    │   │   ├── java
    │   │   │   └── com
    │   │   │       └── learning
    │   │   │           └── welcome
    │   │   │               └── WelcomeAPI.java
    │   │   └── resources
    │   └── test
    │       └── java
    ├── target
    │   ├── classes
    │   │   ├── META-INF
    │   │   │   └── MANIFEST.MF
    │   │   └── com
    │   │       └── learning
    │   │           └── welcome
    │   │               └── WelcomeAPI.class
    │   ├── generated-sources
    │   │   └── annotations
    │   ├── maven-status
    │   │   └── maven-compiler-plugin
    │   │       ├── compile
    │   │       │   └── default-compile
    │   │       │       ├── createdFiles.lst
    │   │       │       └── inputFiles.lst
    │   │       └── testCompile
    │   │           └── default-testCompile
    │   │               └── inputFiles.lst
    │   └── welcome-api-1.0-SNAPSHOT.jar
    └── welcomeapi.iml

您尝试与之互动的 类 不存在。

<bean id="welcomeImpl" class="welcomeimpl.WelcomeImpl"/>
<service ref="welcomeImpl" interface="welcome.WelcomeAPI"/>

应该是

<bean id="welcomeImpl" class="com.learning.welcomeimpl.WelcomeImpl"/>
<service ref="welcomeImpl" interface="com.learning.welcome.WelcomeAPI"/>

并且您没有导出正确的包:

<Export-Package>
    com.learning.welcome;version=${project.version}
</Export-Package>

也在这里修复包或保留 *

您的示例中存在一些问题,但让我们从您的目标开始吧。

在每个包中,您想要导出其他包应该看到的包,并且您想要导入您的包需要的所有包。好消息是 maven-bundle-plugin 在解决这些问题方面做得很好。

对于导入,通常不定义任何内容。该插件几乎总是做正确的事。

对于导出,您的 API 包想要导出 com.learning.welcome 而 impl 包不需要导出任何东西。默认情况下,bundle 插件会导出所有包,除了那些具有特殊名称(如 impl)的包。因此,默认值将再次做正确的事情。只需不要在两个包中定义任何导入和导出,它将是正确的。

最后是蓝图。就像 Alessandro 解释的那样,你在那里使用了错误的包。 Maven bundle 插件还会查看您的蓝图以找出导入。所以它会导入不存在的包 "welcome" ,这当然不会被任何包导出。当我进行重构并且蓝图保持不变时,这个问题也让我很受打击。