Search code examples
javamavenosgiapache-felix

Services not listed in Karaf, what might be the reason?


I'm using combination of R6 OSGi annotations from OSGi Alliance, maven and Apache felix maven-scr-plugin.

After writing a simple bundle I don't see any services inside it (using Karaf webconsole or service:list )

The same works with Low Level API via BundleContext where I manually register a service.

As far as I understand maven-scr-plugin generates for me manifest and component XML files in runtime.

In the code below I would expect service SimpleMathI would be registered in Karaf Service Registry: Did i miss anything?

package test;

//notice i don't use apache.felix, since:
//"Starting with the R6 release of the OSGi Declarative Services and Metatype specification, the official annotations support the same
//features as the Apache Felix SCR annotations in a more elegant manner and even provide additional functionality."

import org.osgi.framework.BundleContext;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;

    @Component
    public class TestClass implements SimpleMathI {
    public TestClass() {
        System.out.println("contructing TestClass");
    }

    @Activate
    protected void activate(ComponentContext c, BundleContext b) {
        System.out.println("activate testClass");
    }

    @Deactivate
    protected void deactivate() {
        System.out.println("de-activate testClass");
    }

    public void doSimpleAdd(int x, int y) {
        System.out.println("Result(TestClass): " + (x + y));
    }

    public void doSimpleSubstract(int x, int y) {
         System.out.println("Result(TestClass): " + (x - y));

    }
}

here is my pom file:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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>
  <groupId>com</groupId>
  <artifactId>DStestDS</artifactId>
  <version>0.0.5</version>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.felix</groupId>
        <artifactId>maven-scr-plugin</artifactId>
        <version>1.20.0</version>
        <executions>
          <execution>
            <id>generate-scr-scrdescriptor</id>
            <goals>
              <goal>scr</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>   
  </build>

   <dependencies>      

    <dependency>
        <groupId>org.osgi</groupId>
        <artifactId>org.osgi.core</artifactId>
         <version>4.3.0</version>
    </dependency>

<!--  official R6 osgi annotations  -->
    <dependency>
        <groupId>org.osgi</groupId>
        <artifactId>org.osgi.service.component.annotations</artifactId>
        <version>1.3.0</version>
    </dependency>

    <dependency>
        <groupId>org.osgi</groupId>
        <artifactId>org.osgi.compendium</artifactId>
        <version>5.0.0</version>
    </dependency>           

    <dependency>
         <groupId>org.apache.felix</groupId>
         <artifactId>org.apache.felix.scr.annotations</artifactId>
         <version>1.9.6</version>
         <scope>provided</scope>
    </dependency>           

   </dependencies>
</project>

Solution

  • Did you maybe forget to install the scr feature?

    feature:install scr

    Your pom also seems to be broken. You need to use the maven-bundle-plugin or the bnd-maven-plugin. If you use the OSGi spec DS annotations then the maven scr plugin is not needed.

    This is what I use in my builds: https://github.com/cschneider/Karaf-Tutorial/blob/master/tasklist-ds/pom.xml#L107-L118

    It creates bundles and also processes DS spec annotations.