java - 在 Karaf 中启动 bundle 时出现 OSGi os.wiring.package 错误

标签 java maven osgi blueprint-osgi

我正在深入研究 OSGi 领域,并构建了一个 bundle ,并尝试将其安装到 Karaf 中。我收到以下错误:

我想知道这是否是蓝图的工作方式,或者我是否以某种方式没有正确导出/导入模块?

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

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>

错误

执行命令时出错: 在 bundle 上执行命令时出错: 启动 bundle 119 时出错:无法解析 impl [119](R 119.0):缺少要求 [impl [119](R 119.0)] osgi.wiring.package; (osgi.wiring.package=welcome) Unresolved 需求:[[impl [119](R 119.0)] osgi.wiring.package; (osgi.wiring.package=欢迎)]

实现 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

最佳答案

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

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

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

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

最后一件事是蓝图。就像亚历山德罗解释的那样,你在那里使用了错误的包。 Maven 捆绑插件还会查看您的蓝图以找出导入。因此它将导入不存在的包“welcome”,当然它不会被任何包导出。当我进行重构并且蓝图保持不变时,这个问题也给我带来了很大的打击。

关于java - 在 Karaf 中启动 bundle 时出现 OSGi os.wiring.package 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48493274/

相关文章:

java - 在 View.OnClick 中更新 RecyclerView 的最佳做法是什么?

java - 如何将 lombok 和 JPAMetalModel 处理器与 maven 合并

maven - 如何从war文件中删除版本号

java - bundle 字段上的引用注释

java - 如何在java中对JSON对象进行排序?

java - 如何在 ThreadPoolExecutor 中保持核心线程存活并淘汰多余线程? keepAliveTime 没有按预期工作

java - 如何签署 Java midlet?

eclipse - Maven:如何在 web.xml 文件中填充变量

ant - 是否有一种简单的方法可以在 Ant junit 或 java 任务的类路径中使用 OSGi 包?

java - 查询 Eclipse 插件中的 jar 条目