q39uxq
Last Updated: February 25, 2016
·
2.324K
· kjellski
Gavatar big

Using scr annotations in a pax-construct project with maven-bundle-plugin

When you start a new osgi project and want to be really IDE independent you'll have a hard time gathering the necessary informations together these days. Here is how I figured these out:

When you created the project by pax-construct, and created the bundle module with bundle-create, you'll end up with a ready to go configuration for BundleActivators as the building blocks of the bundle structure. But what about SCR?
I wanted to leverage the ability to use the corresponding annotations from org.apache.felix.scr. We need to depend on them in the project and then use them for the general configuration when entering the package phase.

First we'll configure the general compilation configuration of the submodules from poms/compiled/pom.xml. Add the plugin right where the maven-bundle-plugin already is:

<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>

Then you're basically set to let the scr plugin generate the needed OSGI.INF files for each of your subprojects. But without telling the maven-bundle-plugin to use these informations and wire them up, we'll see nothing. And this is the part that was hard to firgure. You need to tell it to do this. In the same file, under poms/compiled/pom.xml, look at the instructions for maven-bundle-plugin and add right under the <_include> statement:

<Service-Component>
    OSGI-INF/serviceComponents.xml
</Service-Component>

Now depend on the annotations in the subprojects in order to use them:

<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>

Now define your components with annotations @Component and @Service. When you want none to be included, like in any api bundle, just insert "Service-Component: *" in the osgi.bnd file. This will cause the bundle plugin to stop complaining about the missing OSGI.INF for the project that doesn't contain any.

Hope this helps somebody ;)

Cheers,
Kjellski

Say Thanks
Respond