Search code examples
mavennetbeansosginetbeans-8apache-felix

OSGi bundle doesn't start after inserting a Bundle Activator


I'm implementing a maven web project in Netbeans (version 8.0.2) with some build profiles (jetty, OSGi..) managed by POM file using plugins and custom goals of project in Netbeans.
I need to manage the lifecycle of the generated OSGi bundle, so I created an activator with the following class:

package org.activiti.explorer.conf;


import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

public class OSGiActivator implements BundleActivator {

    @Override
    public void start(BundleContext context) throws Exception {
        System.out.println("BUNDLE STARTED");
    }

    @Override
    public void stop(BundleContext context) throws Exception {
        System.out.println("BUNDLE STOPPED");

    }

}

After that I updated the maven bundle <plugin> element in the POM file adding the informaton about the activator, so the plugin is:

 <plugin>
                        <groupId>org.apache.felix</groupId>
                        <artifactId>maven-bundle-plugin</artifactId>
                        <extensions>true</extensions>
                        <configuration>
                            <supportedProjectTypes>
                                <supportedProjectType>jar</supportedProjectType>
                                <supportedProjectType>bundle</supportedProjectType>
                                <supportedProjectType>war</supportedProjectType>
                            </supportedProjectTypes>
                            <instructions>
                                <Bundle-Activator>org.activiti.explorer.conf.OSGiActivator</Bundle-Activator>
                                <Bundle-SymbolicName>${project.name}</Bundle-SymbolicName>
                                <Bundle-Version>${project.version}</Bundle-Version>
                                <Bundle-ClassPath>.,WEB-INF/classes</Bundle-ClassPath>
                                <Embed-Dependency>*</Embed-Dependency>
                                <Embed-Transitive>true</Embed-Transitive>
                                <Embed-Directory>WEB-INF/lib</Embed-Directory>
                                <Web-ContextPath>activiti-explorer</Web-ContextPath>
                                <Import-Package> org.eclipse.jetty.servlet.listener,
                                    org.apache.jasper.servlet,
                                    javax.naming,
                                    javax.el;resolution:=optional,
                                    com.sun.el;resolution:=optional,
                                    org.apache.el;resolution:=optional,
                                    javax.servlet.jsp;resolution:=optional,
                                    org.apache.jasper.runtime;resolution:=optional,
                                    org.apache.tomcat;resolution:=optional,
                                    javax.servlet;version="[2.6.0,4.0.1]",
                                    javax.servlet.http;version="[2.6.0,4.0.1]",
                                    org.apache.log4j,
                                    javax.inject,
                                    org.slf4j.impl,
                                    org.slf4j,
                                    org.slf4j.spi,
                                    org.apache.commons.io;version="[1.4,2)",
                                    org.apache.commons.io.output;version="[1.4,2)",
                                    org.apache.commons.lang3;version="[3.1,4)",
                                    org.apache.commons.lang3.builder;version="[3.1,4)",
                                    org.apache.commons.lang3.exception;version="[3.1,4)"
                                </Import-Package>
                                <Export-Package>!*</Export-Package>
                            </instructions>
                            <manifestLocation>${basedir}/target/META-INF</manifestLocation>
                        </configuration>
                        <executions>
                            <execution>
                                <id>generate-manifest</id>
                                <phase>process-classes</phase>
                                <goals>
                                    <goal>manifest</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>

Now, when I build the bundle and load it on OSGi with Felix, the bundle is activated but it doesn't start, whereas if I delete the row about the Bundle-Activator from POM file it works properly.
Note that from Felix console no error is displayed about Build classpath, imported packages and exported packages and if I try to start bundle from osgi line command, this exception is logged:

gogo: BundleException: Error starting module.

Can someone help me? If I don't use the class which implements the Activator and I don't refer it in the POM file, the bundle created runs properly in OSGi.


Solution

  • You are listing all the Import-Package statements explicitly. This means that the maven-bundle-plugin does not add auto detected package usages. You are missing the Import-Package for org.osgi.framework. So the bundle resolves but the package is not wired and when loading the OSGiActivator class it fails.

    So the simple solution is to add the package import.

    I rather recommend though to not define all the imports and let the maven bundle plugin figure them out automatically. You can do this by adding a ",*". You should then remove all the packages it figures out by itself.