1
votes

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>
2

2 Answers

2
votes

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.

0
votes

After Christian's suggestion I have added maven-bundle-plugin to the pom.xml file and removed maven-scr-plugin, now the pom.xml looks like below:

<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.10</version>

   <dependencies>   
    <dependency>
        <groupId>org.osgi</groupId>
        <artifactId>org.osgi.core</artifactId>
        <version>4.3.0</version>
    </dependency>    
    <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.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
    </dependency>    
   </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>                
            </plugin>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <version>3.0.0</version>
                <extensions>true</extensions>
                <configuration>
                    <obrRepository>NONE</obrRepository>
                    <instructions>
                        <_include>-bnd.bnd</_include>
                    </instructions>
                </configuration>
            </plugin>
        </plugins>
    </build>   
</project>

And here is the output from the console: no services listed

So I can't understand when and why service gets registered? Why no @Activate method is called?

BTW, I don't get any compile errors, also I don't do "Export->Deployable plug-ins" project, I simply do mvn clean install take the output jar file and put into Karaf's delploy folder.

After generating a bundle file I looked inside it and found that META-INF\MANIFEST.MF looks like:

Manifest-Version: 1.0
Built-By: username
Build-Jdk: 1.7.0_79
Created-By: Apache Maven 3.3.9
Archiver-Version: Plexus Archiver

it seems that something is missing, isn't it?

and I don't see any services listed via karaf webconsole:

karaf webconsole