有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何修复组织。奥斯基。框架BundleException:无法缓存捆绑包?

我正在使用一个OSGi BundleActivator代码。当我试图使用ApacheKaraf安装它时,我总是会遇到一个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 root和bundle目录运行它,但没有帮助

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

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

从不同的目录中尝试了mvn installmvn clean install,没有帮助

卡拉夫是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

提前感谢您的帮助


共 (5) 个答案

  1. # 1 楼答案

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

  2. # 2 楼答案

    我已经有一段时间没有使用卡拉夫了,但我几乎没有什么想法

    IIRC“org.osgi.framework.BundleException:无法缓存捆绑包”实际上意味着Karaf从提供的URL获得的不是可安装的捆绑包。所以

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

    我隐约记得在bundle:install和快照捆绑包和/或本地Maven存储库方面有一些问题,但遗憾的是,没有任何细节。需要检查的一件事是你的Maven repos and setting是正确的

  3. # 3 楼答案

    我知道你提到过它对你没有帮助,但它对我有用,而且可能对其他人有帮助,可以设置位于 <KARAF_HOME>/etc/org.ops4j.pax.url.mvn.cfg文件

  4. # 4 楼答案

    也许有点晚了,但错误表明卡拉夫找不到包裹。在中检查文件位置。如果生成的清单正确,则为m2或。 祝你好运

  5. # 5 楼答案

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

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

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

    把它拿走