【问题标题】:How to fix org.osgi.framework.BundleException: Unable to cache bundle?如何修复 org.osgi.framework.BundleException:无法缓存捆绑包?
【发布时间】:2019-12-20 04:04:17
【问题描述】:

我正在使用 OSGi BundleActivator 代码。当我尝试使用 Apache Karaf 安装它时,我总是收到 Unable to install bundle mvn:com.baeldung/osgi-intro-sample-activator/1.0-SNAPSHOT: org.osgi.framework.BundleException: Unable to cache bundle: mvn:com.baeldung/osgi-intro-sample-activator/1.0-SNAPSHOT 错误。

我尝试遵循的教程在这里:https://www.baeldung.com/osgi

我使用的命令是bundle:install mvn:com.baeldung/osgi-intro-sample-activator/1.0-SNAPSHOT。尝试添加 -s 标志或使用install 而不是bundle:install,没有帮助。尝试从 Karaf 根目录和捆绑目录运行它,但没有帮助。

在一个 Karaf 文件夹中,我尝试设置 org.ops4j.pax.url.mvn.localRepository=/Users/bogdansalyp/.m2/repository,但没有帮助。

清空.m2/repository,没有帮助。将其复制到 bundle 和 Karaf 文件夹,没有帮助。

尝试了来自不同目录的mvn installmvn clean install,没有帮助。

Karaf 是 v4.2.6,maven 是 3.1.1

这是我的 pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
    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>
    <artifactId>osgi-intro-sample-activator</artifactId>
    <name>osgi-intro-sample-activator</name>
    <!-- Please, note this is not the usual 'jar'. -->
    <packaging>bundle</packaging>

    <!-- com.baeldung/osgi-intro-sample-activator/1.0-SNAPSHOT -->
    <parent>
        <artifactId>osgi</artifactId>
        <groupId>com.baeldung</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.core</artifactId>
        </dependency>
    </dependencies>

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

                        <!-- Qualified name of the class that exposes the activator iface. -->
                        <Bundle-Activator>com.baeldung.osgi.sample.activator.HelloWorld</Bundle-Activator>

                        <!-- One important thing to note: since you are not exporting the package "com.baeldung.osgi.sample.activator", you should at least add it to the Private-Package
                            instruction. Otherwise, the classes inside the package will not be copied to your bundle, as the default value of this instruction is empty. -->

                        <Private-Package>com.baeldung.osgi.sample.activator</Private-Package>

                    </instructions>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

这是我使用的 Java 代码:

package com.baeldung.osgi.sample.activator;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

public class HelloWorld implements BundleActivator {

    public void start(BundleContext ctx) {
        System.out.println("Hello World.");
    }

    public void stop(BundleContext bundleContext) {
        System.out.println("Goodbye World.");
    }

}

代码结构可以在这里找到:https://github.com/eugenp/tutorials/tree/master/osgi/osgi-intro-sample-activator

提前感谢您的帮助!

【问题讨论】:

    标签: java osgi apache-karaf osgi-bundle


    【解决方案1】:

    自从我使用 Karaf 已经有一段时间了,但我没有什么想法。

    IIRC “org.osgi.framework.BundleException: Unable to cache bundle”实际上意味着 Karaf 从提供的 URL 获得的不是可安装的包。所以

    • 确保您已运行 mvn installmvn package 不够)在本地 Maven 存储库中安装该包您尝试在 Karaf 中安装它之前。
    • 尝试将版本形式1.0-SHAPSHOT 更改为1.0.0-SNAPSHOT。不确定这是否相关,但一般尝试使用semantic versions
    • 尝试使用 bundle:watch 而不是 bundle:install (see the docs),当您将它重新安装到本地 Maven 存储库时,它也会自动更新捆绑包。

    我隐约记得bundle:install 和 SNAPSHOT 包和/或本地 Maven 存储库存在一些问题,但遗憾的是没有详细信息。要检查的一件事是您的Maven repos and setting 是否正确。

    【讨论】:

      【解决方案2】:

      您应该将 bundle:install mvn:com.baeldung/osgi-intro-sample-activator/1.0-SNAPSHOT 替换为 bundle:install mvn:com.baeldung/osgi/1.0-SNAPSHOT,因为您的 artifactId 是 osgi。继续关注此链接

      【讨论】:

        【解决方案3】:

        如果我没记错的话,激活器不应该位于私有包中,它应该是公共的。

        很可能,这就是罪魁祸首:

        <Private-Package>com.baeldung.osgi.sample.activator</Private-Package>
        

        删除它。

        【讨论】:

        • 很遗憾,情况并没有改变
        【解决方案4】:

        也许有点晚了,但错误说 karaf 找不到捆绑包。检查 .m2 中的文件位置或生成的清单是否正确。 祝你好运!

        【讨论】:

          【解决方案5】:

          我知道您提到它对您没有帮助,但它对我有用,并且可能对其他人设置属性 org.ops4j.pax.url.mvn.localRepository= 位于 &lt;KARAF_HOME&gt;/etc/org.ops4j.pax.url.mvn.cfg 文件。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-08-08
            相关资源
            最近更新 更多