未列入卡拉夫的服务,可能是什么原因?

时间:2016-10-13 10:09:46

标签: java maven osgi apache-felix

我使用了来自OSGi联盟的 OSGi注释 maven 和Apache felix maven-scr-plugin 的组合。< / p>

编写简单的捆绑包后,我看不到其中的任何服务(使用Karaf网络控制台或服务:列表)

同样适用于通过BundleContext的低级API,我手动注册服务。

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

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

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>

2 个答案:

答案 0 :(得分:2)

您是否忘记安装scr功能?

import React from 'react'; class NotFound extends React.Component { render() { return ( <h2>Not Found!111!!</h2> ) } } export default NotFound;

你的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规范注释。

答案 1 :(得分:0)

在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方法?

BTW,我没有得到任何编译错误,我也没做“Export-&gt; Deployable plug-ins”项目,我只是做 mvn clean install 取输出jar文件并放入Karaf的delploy文件夹。

生成一个bundle文件后,我查看了它,发现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