Maven SCR插件 - 不生成OSGI-INF文件夹

时间:2013-04-17 15:53:07

标签: maven osgi apache-felix

我一直无法使用我的SCR插件。我已经搜索了尽可能多的只能找到与我需要使用的结构不相似的例子。下面的POM片段。这几乎是CQ项目原型生成的默认值。所有依赖都在那里,所以可能不是那样。这是构建的输出:

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

输出的关键部分是[INFO] --- maven-scr-plugin:1.7.4:scr (generate-scr-descriptor) @ project-bundle ---。即使类上存在注释,它似乎也没有生成任何内容。

父POM代码段

<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代码段

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

正在构建代码:

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);
    }
}

非常感谢任何帮助。

3 个答案:

答案 0 :(得分:7)

我猜你正在使用<packaging>bundle</packaging>,如果是这样的话,我通过将maven-scr-plugin升级到1.9.0,使用org.apache.felix.scr.annotations @ 1.7.0 < / p>

我在这里放了一个工作版的tarball:https://dl.dropboxusercontent.com/u/2465717/com.stackoverflow.osgi.scr.tgz

注意:您可以启用Eclipse m2e生命周期映射 - 它运行良好(Juno SR2),在正常的Problems控制台中生成SCR组件错误。将action <ignore/>更改为:

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

同样,虽然规范表明SCR运行时将搜索重载的de / activate方法 - 作为个人偏好,我会在各自的方法上使用@Activate,@ Deactive和@Modified来使意图显式化(在代码和生成的XML中) 。但每个都是他们自己的。

答案 1 :(得分:1)

我建议你

  • 升级到maven-scr-plugin
  • 的最新版本
  • 使用-X生成调试,例如mvn -X compile

答案 2 :(得分:0)

升级到1.9.0对我有用。我们是说插件v1.7.4不生成描述符。为什么会如此,这不是maven-scr-plugin的核心任务。