1

我正在尝试使用 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</featurekarat-maven-plugin's <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>

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

谁能给我一些关于如何解决我的问题的提示?

史蒂夫

4

2 回答 2

2

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

我目前对一个更好的可能世界的看法(即我不会因为这样的问题浪费 4 天时间)包括:

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

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

于 2017-03-28T23:49:50.920 回答
0

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

此外,根据 OSGi 的版本,在属性上使用 @Reference 可能不起作用,您可能需要对其使用 getter/setter(绑定/取消绑定)方法。

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

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

于 2017-03-28T06:54:32.577 回答