如何从头开始使用org.apache.felix.scr注释?

Kje*_*ski 3 java osgi maven apache-felix bnd

我已经开始为apache felix开发一个包,并使用ops4j pax插件.

我创建了项目结构,pax-create-project并且在那里做了正常的pax-create-bundle.然后你得到了用maven构建整个事物的初始项目结构.这里的重要部分是,你的bundle拥有它自己的pom(bundlename/pom.xml)和bnd文件(bundlename/osgi.bnd),但是maven-bundle-plugin已经提供了配置poms/compiled/pom.xml.捆绑元数据在下配置,poms/compiled/pom.xml但标准激活器在上述osgi.bnd文件下配置.bnd文件中的默认值是Bundle-Activator: ${bundle.namespace}.internal.ExampleActivator.

现在我想开始使用注释,org.apache.felix.scr所以我把它包含在捆绑包自己的pom下依赖:

<dependency>
  <groupId>org.apache.felix</groupId>
  <artifactId>org.apache.felix.scr</artifactId>
  <version>1.6.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

我创建了我的服务接口:

package namespace;

public interface Sample {
  void sayHello();
}
Run Code Online (Sandbox Code Playgroud)

以及来自org.apache.felix.scr以下注释的实现:

package namespace.internal;

import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import namespace.Sample;

@Component
@Service
public class SampleImpl implements Sample{

  @Activate
  void start(){
    System.out.println("Started SampleImpl.");
  }

  @Deactivate
  void stop(){
    System.out.println("Stopped SampleImpl.");
  }

  @Override
  public void sayHello() {
    System.out.println("Hello!");
  }
}
Run Code Online (Sandbox Code Playgroud)

我需要在osgi.bnd文件中放置什么才能启动组件?我不得不删除上面提到的默认条目,因为我不再使用BundleActivator了.

现在问题是:我需要对上述任何文件做些什么才能mvn clean install pax:provision启动一个启动我的捆绑包的felix环境并让我看到任何打印输出?我已经查看了Apache Log Service的日志输出,我也开始了,它只是告诉我:

2012.09.28 16:52:44 INFO - Bundle: namespace - BundleEvent STARTED
2012.09.28 16:52:44 INFO - Bundle: namespace - BundleEvent RESOLVED
Run Code Online (Sandbox Code Playgroud)

任何提示都非常受欢迎.或链接,我已经搜索了几个小时,找到一个完整的指南,使用scr和捆绑在一起的maven插件.

问候,Kjellski

Kje*_*ski 5

这是我对此主题的coderwall写作的修改:http://coderwall.com/p/q39uxq

首先从poms/compiled/pom.xml配置子模块的常规编译配置.将插件添加到maven-bundle-plugin已经存在的位置:

<plugin>
    <groupId>org.apache.felix</groupId>
    <artifactId>maven-scr-plugin</artifactId>
    <version><timeofwriting:1.8.0></version>
    <executions>
        <execution>
            <id>generate-scr-scrdescriptor</id>
            <goals>
                <goal>scr</goal>
            </goals>
        </execution>
    </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)

在同一个文件中,在poms/compiled/pom.xml下,查看maven-bundle-plugin的说明并在<_include>语句下添加:

<Service-Component>
    OSGI-INF/serviceComponents.xml
</Service-Component>
Run Code Online (Sandbox Code Playgroud)

现在依赖子项目中的注释来使用它们:

<dependency>
    <groupId>org.apache.felix</groupId>
    <artifactId>org.apache.felix.scr</artifactId>
    <version><timeofwriting:1.6.0></version>
</dependency>
<dependency>
    <groupId>org.apache.felix</groupId>
    <artifactId>
        org.apache.felix.scr.annotations
    </artifactId>
    <version><timeofwriting:1.7.0></version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

现在使用注释@Component@Service定义组件.当您不想包含任何内容时,就像在任何api包中一样,只需在osgi.bnd文件中插入"Service-Component:*"即可.这将导致bundle插件停止抱怨不包含any的项目缺少OSGI.INF.

希望这有助于某人;)

干杯,Kjellski