Karaf 中没有列出服务,可能是什么原因?

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

我正在使用来自 OSGi Alliance、maven 和 Apache felix maven-scr 的 R6 OSGi 注释的组合-插件.

写完一个简单的包后,我在里面看不到任何服务(使用 Karaf webconsole 或 service:list)

低级别 API 同样适​​用于 BundleContext,我在其中手动注册了一个服务。

据我了解 maven-scr-plugin 在运行时为我生成清单和组件 XML 文件。

在下面的代码中,我希望服务 SimpleMathI 会在 Karaf 服务注册表中注册: 我错过了什么吗?

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));

    }
}

这是我的 pom 文件:

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

您是否忘记安装 scr 功能?

feature:install scr

你的pom好像也坏了。您需要使用 maven-bundle-plugin 或 bnd-maven-plugin。如果您使用 OSGi 规范 DS 注释,则不需要 maven scr 插件。

这是我在构建中使用的: https://github.com/cschneider/Karaf-Tutorial/blob/master/tasklist-ds/pom.xml#L107-L118

它创建包并处理 DS 规范注释。

根据 Christian 的建议,我将 maven-bundle-plugin 添加到 pom.xml 文件并删除了 maven-scr-plugin,现在 pom.xml 如下所示:

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

这是控制台的输出: no services listed

所以我无法理解何时以及为何注册服务?为什么没有调用@Activate 方法?

顺便说一句,我没有得到任何编译错误,我也不做 "Export->Deployable plug-ins" 项目,我只是做 mvn clean install 获取输出 jar 文件并放入Karaf的delploy文件夹中。

生成捆绑文件后,我查看了它,发现 META-INF\MANIFEST.MF 看起来像:

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

好像少了点什么吧?

而且我没有看到通过 karaf webconsole 列出的任何服务:

karaf webconsole