在Karaf 4.1.0中使用声明性服务

时间:2017-03-27 14:01:51

标签: maven karaf declarative-services karaf-maven-plugin

我正在尝试使用NetBeans 8.2,Maven 3.3.9和声明性服务开发Karaf 4.1.0应用程序。真正简单的服务可以工作,但是一旦我尝试做一些模糊有用的事情,我就会遇到可怕的osgi.component缺少需求错误。

以下说明我遇到的问题:

package net.winnall.enocean.bridge.sass.impl;

import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.winnall.enocean.bridge.sass.SASS;
import org.osgi.service.http.HttpService;

@Component(
        service = SASS.class
)
public class SASSImpl implements SASS {

    @Reference
    HttpService httpService;

    @Activate
    protected void activate() {
    }

    @Deactivate
    }
}

如果我注释掉@Reference组件被加载到生成的Karaf组件中而没有任何问题。但是当组件位于此处(使用@Reference)时,我收到以下错误:

Failed to execute goal org.apache.karaf.tooling:karaf-maven-plugin:4.1.0:assembly (default-assembly) on project EnOceanBridgeAdmin: Unable to build assembly: Unable to resolve root: missing requirement [root] osgi.identity; osgi.identity=EnOceanBridgeSASSFeature; type=karaf.feature; version=0.99.99; filter:="(&(osgi.identity=EnOceanBridgeSASSFeature)(type=karaf.feature)(version>=0.99.99))" [caused by: Unable to resolve EnOceanBridgeSASSFeature/0.99.99: missing requirement [EnOceanBridgeSASSFeature/0.99.99] osgi.identity; osgi.identity=EnOceanBridgeSASS.Impl; type=osgi.bundle; version="[0.99.99,0.99.99]"; resolution:=mandatory [caused by: Unable to resolve EnOceanBridgeSASS.Impl/0.99.99: missing requirement [EnOceanBridgeSASS.Impl/0.99.99] osgi.extender; filter:="(&(osgi.extender=osgi.component)(version>=1.3.0)(!(version>=2.0.0)))"]] -> [Help 1]

This question建议安装scr

feature:install scr

因此我尝试将<feature>scr</feature添加到karat-maven-plugin&#39; <bootFeatures>,但这没有任何区别。

以下是此组件的有效POM摘录:

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.apache.felix</groupId>
        <artifactId>org.apache.felix.configadmin</artifactId>
        <version>1.8.14</version>
      </dependency>

      <dependency>
        <groupId>org.apache.felix</groupId>
        <artifactId>org.apache.felix.scr.ds-annotations</artifactId>
        <version>1.2.8</version>
      </dependency>

      <dependency>
        <groupId>org.ops4j.pax.logging</groupId>
        <artifactId>pax-logging-service</artifactId>
        <version>1.9.1</version>
      </dependency>

      <dependency>
        <groupId>org.ops4j.pax.logging</groupId>
        <artifactId>pax-logging-api</artifactId>
        <version>1.9.1</version>
      </dependency>

      <dependency>
        <groupId>org.ops4j.pax.logging</groupId>
        <artifactId>pax-logging-log4j2</artifactId>
        <version>1.9.1</version>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <dependencies>
    <dependency>
      <groupId>org.apache.karaf.features</groupId>
      <artifactId>framework</artifactId>
      <version>4.1.0</version>
      <type>kar</type>
      <scope>compile</scope>
    </dependency>

    <dependency>
      <groupId>org.apache.karaf.features</groupId>
      <artifactId>standard</artifactId>
      <version>4.1.0</version>
      <type>xml</type>
      <classifier>features</classifier>
      <scope>runtime</scope>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.karaf.tooling</groupId>
          <artifactId>karaf-maven-plugin</artifactId>
          <version>4.1.0</version>
          <extensions>true</extensions>
        </plugin>
      </plugins>
    </pluginManagement>

    <plugins>
      <plugin>
        <groupId>org.apache.karaf.tooling</groupId>
        <artifactId>karaf-maven-plugin</artifactId>
        <version>4.1.0</version>
        <extensions>true</extensions>
        <configuration>
          <installedFeatures></installedFeatures>
          <startupFeatures></startupFeatures>
          <bootFeatures>
            <feature>minimal</feature>
            <feature>scr</feature>
          </bootFeatures>
          <javase>1.8</javase>
        </configuration>
      </plugin>

我用来让Karaf装配知道的功能是:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<features xmlns="http://karaf.apache.org/xmlns/features/v1.4.0" name="EnOceanBridgeSASS.Impl">
    <feature name="EnOceanBridgeSASS.Impl" description="EnOceanBridge SASS Impl" version="0.99.99">
        <details>Karaf :: Declarative Services :: Service :: EnOceanBridge SASS Implementation</details>
        <bundle start-level="80">mvn:net.winnall.enocean.service.api/EnOceanBridgeSASS.API/0.99.99</bundle>
        <bundle start-level="80">mvn:org.apache.felix/org.apache.felix.configadmin/1.8.14</bundle>
        <bundle start-level="80">mvn:org.ops4j.pax.logging/pax-logging-api/1.9.1</bundle>
        <bundle start-level="80">mvn:org.ops4j.pax.logging/pax-logging-service/1.9.1</bundle>
    </feature>
</features>

卡拉夫大会的有效POM包含了这个:

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.apache.felix</groupId>
        <artifactId>org.apache.felix.configadmin</artifactId>
        <version>1.8.14</version>
      </dependency>

      <dependency>
        <groupId>org.apache.felix</groupId>
        <artifactId>org.apache.felix.scr.ds-annotations</artifactId>
        <version>1.2.8</version>
      </dependency>

      <dependency>
        <groupId>org.ops4j.pax.logging</groupId>
        <artifactId>pax-logging-service</artifactId>
        <version>1.9.1</version>
      </dependency>

      <dependency>
        <groupId>org.ops4j.pax.logging</groupId>
        <artifactId>pax-logging-api</artifactId>
        <version>1.9.1</version>
      </dependency>

      <dependency>
        <groupId>org.ops4j.pax.logging</groupId>
        <artifactId>pax-logging-log4j2</artifactId>
        <version>1.9.1</version>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <dependencies>
    <dependency>
      <groupId>net.winnall.enocean.feature</groupId>
      <artifactId>EnOceanBridgeSettingsFeature</artifactId>
      <version>0.99.99</version>
      <type>xml</type>
      <classifier>features</classifier>
      <scope>compile</scope>
    </dependency>

    <dependency>
      <groupId>net.winnall.enocean.feature</groupId>
      <artifactId>EnOceanBridgeSASSFeature</artifactId>
      <version>0.99.99</version>
      <type>xml</type>
      <classifier>features</classifier>
      <scope>compile</scope>
    </dependency>

    <dependency>
      <groupId>net.winnall.enocean.feature</groupId>
      <artifactId>EnOceanBridgePersistenceFeature</artifactId>
      <version>0.99.99</version>
      <type>xml</type>
      <classifier>features</classifier>
      <scope>compile</scope>
    </dependency>

    <dependency>
      <groupId>org.apache.karaf.features</groupId>
      <artifactId>framework</artifactId>
      <version>4.1.0</version>
      <type>kar</type>
      <scope>compile</scope>
    </dependency>

    <dependency>
      <groupId>org.apache.karaf.features</groupId>
      <artifactId>standard</artifactId>
      <version>4.1.0</version>
      <type>xml</type>
      <classifier>features</classifier>
      <scope>runtime</scope>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.karaf.tooling</groupId>
          <artifactId>karaf-maven-plugin</artifactId>
          <version>4.1.0</version>
          <extensions>true</extensions>
        </plugin>

        <plugin>
          <artifactId>maven-archetype-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>

        <plugin>
          <groupId>com.github.ferstl</groupId>
          <artifactId>depgraph-maven-plugin</artifactId>
          <version>2.1.0</version>
        </plugin>

        <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>exec-maven-plugin</artifactId>
          <version>1.6.0</version>
        </plugin>

        <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>license-maven-plugin</artifactId>
          <version>1.12</version>
        </plugin>

        <plugin>
          <artifactId>maven-antrun-plugin</artifactId>
          <version>1.8</version>
        </plugin>

        <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>2.2-beta-5</version>
        </plugin>

        <plugin>
          <artifactId>maven-dependency-plugin</artifactId>
          <version>2.1</version>
        </plugin>

        <plugin>
          <artifactId>maven-release-plugin</artifactId>
          <version>2.0</version>
        </plugin>

        <plugin>
          <groupId>org.apache.felix</groupId>
          <artifactId>maven-bundle-plugin</artifactId>
          <version>3.3.0</version>
        </plugin>

        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
      </plugins>
    </pluginManagement>

    <plugins>
      <plugin>
        <groupId>com.github.ferstl</groupId>
        <artifactId>depgraph-maven-plugin</artifactId>
        <version>2.1.0</version>
      </plugin>

      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>3.0.2</version>
        <executions>
          <execution>
            <id>default-resources</id>
            <phase>process-resources</phase>
            <goals>
              <goal>resources</goal>
            </goals>
          </execution>
          <execution>
            <id>process-resources</id>
            <goals>
              <goal>resources</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

      <plugin>
        <groupId>org.apache.karaf.tooling</groupId>
        <artifactId>karaf-maven-plugin</artifactId>
        <version>4.1.0</version>
        <extensions>true</extensions>
        <executions>
          <execution>
            <id>default-archive</id>
            <phase>package</phase>
            <goals>
              <goal>archive</goal>
            </goals>
            <configuration>
              <installedFeatures></installedFeatures>
              <startupFeatures></startupFeatures>
              <bootFeatures>
                <feature>minimal</feature>
                <feature>scr</feature>
              </bootFeatures>
              <javase>1.8</javase>
            </configuration>
          </execution>

          <execution>
            <id>default-assembly</id>
            <phase>process-resources</phase>
            <goals>
              <goal>assembly</goal>
            </goals>
            <configuration>
              <installedFeatures></installedFeatures>
              <startupFeatures></startupFeatures>
              <bootFeatures>
                <feature>minimal</feature>
                <feature>scr</feature>
              </bootFeatures>
              <javase>1.8</javase>
            </configuration>
          </execution>
        </executions>

        <configuration>
          <installedFeatures></installedFeatures>
          <startupFeatures></startupFeatures>
          <bootFeatures>
            <feature>standard</feature>
            <feature>scr</feature>
          </bootFeatures>
          <javase>1.8</javase>
        </configuration>
      </plugin>

我整个周末都在搜索这个问题:在我看来,互联网上几乎没有任何关于在Karaf使用声明性服务的文档。

有人可以给我一些关于如何解决问题的提示吗?

史蒂夫

2 个答案:

答案 0 :(得分:1)

我终于摆脱了这个错误 - 说实话 - 我不知道我做了什么来修复它。使该工作正常工作的最终编辑是从我上面列出的功能文件中删除某些内容,当我报告初始问题时(这是import tweepy from tweepy import OAuthHandler access_token='your_api_token' access_secret='your_api_access_secret' consumer_key = 'your_api_key' consumer_secret = 'your_consumer_key' auth = OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_secret) api = tweepy.API(auth) class MyListener(StreamListener): def on_status(self, status): try: if status.user.followers_count > 5000: print '%s (%s at %s, followers: %d)' % (status.text, status.user.screen_name, status.created_at, status.user.followers_count) return True except BaseException as e: print("Error on_status: %s" % str(e)) return True def on_error(self, status): print(status) return True twitter_stream = Stream(auth, MyListener()) twitter_stream.filter(track=['$DB','$MS','$C']) <repository />)我徒劳地尝试强制安装HttpService而添加的引用......)。

我目前对一个更好的世界的看法(即我不会在这样的问题上失去4天)包括:

  1. 帮助程序员隔离错误的OSGi错误消息;
  2. 告诉Google我真的只对声明服务的最新版本(即基于注释而非原始BND或XML文件)感兴趣;
  3. 更清晰的基于注释的DS文档没有插图,介绍如何在Eclipse中执行此操作,因为我不使用Eclipse而不想;
  4. 关于如何在Karaf中使用DS的更简单的文档;
  5. 一个Karaf IDE(这是Karaf Boot的用途吗?)。
  6. 我认为Karaf和DS都是很酷的工作方式。我希望它更容易。

答案 1 :(得分:0)

您正在尝试获取有关HTTP服务的参考,但如果您已安装该功能,则不会说明。

同样取决于在属性上使用@Reference的OSGi版本可能不起作用,您可能需要使用getter / setter(bind / unbind)方法。

请参阅http://blog.vogella.com/2016/06/21/getting-started-with-osgi-declarative-services/第7章.DS注释 (感谢Lars Vogel为这个伟大的教程。)

从OSGi开始,enroute项目也是一个很棒的地方。