Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven SCR Plugin - Not generating OSGI-INF folder

I have been having trouble getting my SCR plugin working. I have searched as much as I could bur only found examples that were not similar to the structure I am required to use. Snippets of POMs below. These are pretty much the default generated by the CQ project archetype. All dependencies are there, so it is probably not that. Here is the output of the build:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building project Bundle 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ project-bundle ---
[INFO] Deleting C:\project-path\target
[INFO] 
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ bundle ---
[debug] execute contextualize
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\project-path\src\main\resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ project-bundle ---
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!
[INFO] Compiling 4 source files to C:\project-path\target\classes
[INFO] 
[INFO] --- maven-scr-plugin:1.7.4:scr (generate-scr-descriptor) @ project-bundle ---
[INFO] 
[INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ project-bundle ---
[debug] execute contextualize
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\project-path\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ project-bundle ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-surefire-plugin:9c6abc2:test (default-test) @ project-bundle ---
[INFO] No tests to run.
[INFO] 
[INFO] --- maven-bundle-plugin:2.3.6:bundle (default-bundle) @ project-bundle ---
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ project-bundle ---
[INFO] Installing C:\project-path\target\project-bundle-1.0-SNAPSHOT.jar to C:\user-path\.m2\repository\com\project\cq\project-bundle\1.0-SNAPSHOT\project-bundle-1.0-SNAPSHOT.jar
[INFO] Installing C:\project-path\pom.xml to C:\user-path\.m2\repository\com\project\cq\project-bundle\1.0-SNAPSHOT\project-bundle-1.0-SNAPSHOT.pom
[INFO] 
[INFO] --- maven-bundle-plugin:2.3.6:install (default-install) @ project-bundle ---
[INFO] Installing com/project/cq/project-bundle/1.0-SNAPSHOT/project-bundle-1.0-SNAPSHOT.jar
[INFO] Writing OBR metadata
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.189s
[INFO] Finished at: Wed Apr 17 10:29:17 CDT 2013
[INFO] Final Memory: 15M/221M
[INFO] ------------------------------------------------------------------------

The key part of the output is the [INFO] --- maven-scr-plugin:1.7.4:scr (generate-scr-descriptor) @ project-bundle ---. It appears that it is not generating anything even though the annotations exist on the class.

Parent POM Snippet:

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-scr-plugin</artifactId>
                <version>1.7.4</version>
            </plugin>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <version>2.3.6</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <!-- use version 2.3.2 to have java 1.5 as the default -->
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
                <version>2.3.2</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.5</version>
            </plugin>
            <plugin>
                <groupId>org.apache.sling</groupId>
                <artifactId>maven-sling-plugin</artifactId>
                <version>2.0.6</version>
            </plugin>
            <plugin>
                <groupId>com.day.jcr.vault</groupId>
                <artifactId>content-package-maven-plugin</artifactId>
                <version>0.0.13</version>
            </plugin>
            <!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the 
                Maven build itself. -->
            <plugin>
                <groupId>org.eclipse.m2e</groupId>
                <artifactId>lifecycle-mapping</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <lifecycleMappingMetadata>
                        <pluginExecutions>
                            <pluginExecution>
                                <pluginExecutionFilter>
                                    <groupId>org.apache.felix</groupId>
                                    <artifactId>maven-scr-plugin</artifactId>
                                    <versionRange>[1.7.4,)</versionRange>
                                    <goals>
                                        <goal>scr</goal>
                                    </goals>
                                </pluginExecutionFilter>
                                <action>
                                    <ignore />
                                </action>
                            </pluginExecution>
                        </pluginExecutions>
                    </lifecycleMappingMetadata>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

Child POM Snippet:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-scr-plugin</artifactId>
            <extensions>true</extensions>
            <executions>
                <execution>
                    <id>generate-scr-descriptor</id>
                    <goals>
                        <goal>scr</goal>
                    </goals>
                </execution>
            </executions>
       </plugin>
       <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <extensions>true</extensions>
            <configuration>
                <instructions>
                    <Bundle-SymbolicName>project.project-bundle</Bundle-SymbolicName>
                </instructions>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.sling</groupId>
            <artifactId>maven-sling-plugin</artifactId>
            <configuration>
                <slingUrl>http://${crx.host}:${crx.port}/apps/project/install</slingUrl>
                <usePut>true</usePut>
            </configuration>
        </plugin>
    </plugins>
</build>

Code being built:

package com.project.cq.examples.dictionaryservice.impl;

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.osgi.service.component.ComponentContext;

import com.project.cq.examples.dictionaryservice.DictionaryService;

@Component(immediate = true, metatype = true, label = "Service Implementation", description = "The implementation for the Service")
@Service
public class DictionaryServiceServletImpl implements DictionaryService {

    @Property(label = "Words", description = "The list of words.")
    private static final String PROP_WORDS = "words";

    // The set of words
    private String[] words;
    private boolean active;
    private boolean modified;

    @Override
    public boolean checkWord(String word) {
        word = word.toLowerCase();

        // This is very inefficient
        for (int i = 0; i < words.length; i++) {
            if (words[i].equals(word)) {
                return false;
            }
        }
        return true;
    }

    protected void activate(ComponentContext context) {
        active = true;
        dictionary = (String[]) context.getProperties().get(
                PROP_WORDS);
    }

    protected void deactivate(ComponentContext context) {
        dictionary = null;
        active = false;
    }

    protected void modified(ComponentContext context) {
        modified = true;
        dictionary = (String[]) context.getProperties().get(
                PROP_WORDS);
    }
}

Any help is greatly appreciated.

like image 308
Paul B. Avatar asked Apr 17 '13 15:04

Paul B.


2 Answers

I'm guessing you're using <packaging>bundle</packaging>, if so I got this working by upgrading maven-scr-plugin to 1.9.0, with org.apache.felix.scr.annotations @ 1.7.0

I put a tarball of a working version here: https://dl.dropboxusercontent.com/u/2465717/com.stackoverflow.osgi.scr.tgz

Note: you can enable the Eclipse m2e lifecycle mapping - it works well (Juno SR2), generating SCR component errors in the normal Problems console. Change the action <ignore/> to:

<action>
        <execute>
                <runOnIncremental>true</runOnIncremental>
                <runOnConfiguration>true</runOnConfiguration>
        </execute>
</action>

Also while the spec indicates the SCR runtime will search for overloaded de/activate methods - as personal preference I would use @Activate, @Deactive and @Modified on the respective methods to make the intent explicit (in code and generated XML). But each to their own.

like image 65
earcam Avatar answered Nov 05 '22 07:11

earcam


I suggest that you

  • upgrade to the latest version of the maven-scr-plugin
  • generate debug with -X, e.g. mvn -X compile
like image 1
Robert Munteanu Avatar answered Nov 05 '22 06:11

Robert Munteanu