在 Karaf 4.1.0 中使用声明式服务

Using declarative services in Karaf 4.1.0

我正在尝试使用 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]

建议安装 scr:

feature:install scr

所以我尝试将 <feature>scr</feature 添加到 karat-maven-plugin<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>

Karaf 程序集的有效 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>

我整个周末都在谷歌上搜索这个问题:在我看来,Internet 上几乎没有关于在 Karaf 中使用声明式服务的当前文档。

任何人都可以给我一些解决问题的提示吗?

史蒂夫

您正在尝试获取有关 HTTP 服务的参考,但您没有说明您是否安装了该功能。

另外,根据 OSGi 的版本,在属性上使用 @Reference 可能不起作用,您可能需要为此使用 getter/setter (bind/unbind) 方法。

参见 http://blog.vogella.com/2016/06/21/getting-started-with-osgi-declarative-services/ 第 7 章 DS 注释 (感谢 Lars Vogel 的精彩教程。)

enroute 项目也是开始使用 OSGi 的好地方。

我终于摆脱了这个错误,不过 - 老实说 - 我不确定我做了什么来修复它。使事情起作用的最后编辑是从上面列出的功能文件中删除了一些当我报告最初的问题时根本不存在的东西(这是一个 <repository /> 和一个 <feature /> 参考我添加了徒劳的尝试强制安装 HttpService...)。

我目前对更美好世界的看法(即我不会在此类问题上浪费 4 天时间)包括:

  1. 帮助程序员隔离错误的 OSGi 错误消息;
  2. 一种告诉 Google 我真的只对声明性服务的最新形式感兴趣的方式(即基于注释的而不是原始 BND 或 XML 文件);
  3. 更干净的基于注释的 DS 文档没有说明如何在 Eclipse 中执行此操作,因为我不使用 Eclipse 也不想使用;
  4. 关于如何在 Karaf 中使用 DS 的更多简单文档;
  5. A Karaf IDE(这就是 Karaf Boot 吗?)。

我认为 Karaf 和 DS 都是很酷的工作方式。我希望它更容易。