4

I'm working with an OSGi BundleActivator code. When I'm trying to install it using Apache Karaf I always get a 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 error.

The tutorial I tried to follow is here: https://www.baeldung.com/osgi

The command I use is bundle:install mvn:com.baeldung/osgi-intro-sample-activator/1.0-SNAPSHOT. Tried to add -s flag or use install instead of bundle:install, didn't help. Tried to run it from the Karaf root and bundle directory, didn't help.

In a Karaf folder I tried to set org.ops4j.pax.url.mvn.localRepository=/Users/bogdansalyp/.m2/repository, didn't help.

Emptied .m2/repository, didn't help. Copied it to the bundle and Karaf folders, didn't help.

Tried mvn install and mvn clean install from different directories, didn't help.

Karaf is v4.2.6, maven is 3.1.1

Here's my 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>

Here's the Java code I use:

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.");
    }

}

The code structure can be found here: https://github.com/eugenp/tutorials/tree/master/osgi/osgi-intro-sample-activator

Thanks in advance for your help!

Bogdan Salyp
  • 43
  • 1
  • 7

5 Answers5

3

You should replace with bundle:install mvn:com.baeldung/osgi-intro-sample-activator/1.0-SNAPSHOT to bundle:install mvn:com.baeldung/osgi/1.0-SNAPSHOT because your artifactId is osgi. Keep following this link

Mikalai Lushchytski
  • 1,563
  • 1
  • 9
  • 18
2

It's been a while since I used Karaf but I have few thoughts.

IIRC the "org.osgi.framework.BundleException: Unable to cache bundle" actually means that what Karaf is getting form the provided URL is not an installable bundle. So

  • make sure you have run mvn install (mvn package is not enough) to install the bundle in your local Maven repo before you try to install it in Karaf.
  • try changing the version form 1.0-SHAPSHOT to 1.0.0-SNAPSHOT. Not sure if that is related but in general try to have semantic versions
  • try using bundle:watch instead of bundle:install (see the docs) which will also automatically update the bundle when you reinstall it to local Maven repo.

I vaguely remember having some issues with bundle:install and SNAPSHOT bundles and/or local Maven repository but sadly no details. One thing to check is that your Maven repos and setting are correct.

Milen Dyankov
  • 2,972
  • 14
  • 25
0

If I am not mistaken, the activator should be not be located in the private package, it should be public.

Probably, this is the culprit:

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

Remove it.

Marcos Zolnowski
  • 2,751
  • 1
  • 24
  • 29
0

Maybe it's a little to late, but the error says that karaf can not find the bundle. Check the file location in .m2 or the if the generated MANIFEST is correct. Best luck!

0

I know you've mentioned that it didn't help you, but it worked for me and might be helpful for others to set the property org.ops4j.pax.url.mvn.localRepository= located in <KARAF_HOME>/etc/org.ops4j.pax.url.mvn.cfg file.

IKo
  • 4,998
  • 8
  • 34
  • 54