2
votes

I'm new to OSGI (sorry) and having a few issues trying to deploy my package and related dependencies.

This is my POM:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.felix.test</groupId>
    <artifactId>com.felix.test</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>bundle</packaging>

    <dependencies>
        <dependency>
            <groupId>org.apache.felix</groupId>
            <artifactId>org.osgi.core</artifactId>
            <version>1.0.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.felix</groupId>
            <artifactId>org.apache.felix.scr.annotations</artifactId>
            <version>1.9.6</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
            <version>2.10.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.4</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.5</source>
                    <target>1.5</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <version>2.5.4</version>
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <Export-Package>
                            com.felix.test.search
                        </Export-Package>
                        <Bundle-SymbolicName>
                            ${project.artifactId}
                        </Bundle-SymbolicName>
                        <Bundle-Activator>
                            com.felix.test.Activator
                        </Bundle-Activator>
                    </instructions>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Then I'm bundling this using the Maven command:

mvn org.apache.felix:maven-bundle-plugin:bundleall

This is successful and generates my bundle as well as 3 dependency bundles:

  • net.sf.ehcache_2.10.0.jar
  • org.apache.commons.lang3_3.4.0.jar
  • slf4j.api_1.7.7.jar

This seems OK and I can install and start the first two but when I try and start slf4j I get the following exception:

org.osgi.framework.BundleException: Unable to resolve slf4j.api [25](R 25.0): missing requirement [slf4j.api [25](R 25.0)] osgi.wiring.package; (&(osgi.wiring.package=org.slf4j.impl)(version>=1.6.0)) Unresolved requirements: [[slf4j.api [25](R 25.0)] osgi.wiring.package; (&(osgi.wiring.package=org.slf4j.impl)(version>=1.6.0))]

I'm pretty sure I'm missing something very simple but can't pin it down. Any help would be much appreciated!

3

3 Answers

6
votes

Slf4j has an unusual design (some might say a bad design, ahem). It is an API bundle that depends on an implementation package, namely org.slf4j.impl.

You need to install an additional bundle that implements the Slf4j API. There are lots of choices here... for example slf4j-simple is a basic implementation, whereas slf4j-jdk14 uses the Java 1.4 java.util.logging back end, etc.

Logback also contains an implementation of the API.

1
votes

Need to correct myself as slf4j indeed provides bundles now as Neil pointed out. Not sure how well it works though. I found some explanation how to install slf4j for OSGi here. It does not look very clean to me though. You need to create a bundle fragment for the configuration. Which means you can not change it at runtime.

So I still would rather recommend to use pax-logging at runtime instead. It implements the slf4j api as well as other logging APIs. As backend it uses log4j and configures it via config admin. So you do not need hacks for the logging config and can change it at runtime.

0
votes

I had this same error message, and found out that it was due to bad bundle plugin configuration (as mentioned by @Christian_Schneider).

My situation

So i had roughly this error message when deploying on Karaf:

missing requirement ... Unresolved requirements: ... osgi.wiring.package=org.slf4j.impl

My modules pom.xml looked like this:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>${slf4j_version}</version>
    <scope>provided</scope>
</dependency>

...

<plugin>
    <groupId>org.apache.felix</groupId>
    <artifactId>maven-bundle-plugin</artifactId>
    <configuration>
        <instructions>
            <Private-Package>*</Private-Package>
        </instructions>
    </configuration>
</plugin>

Solution

credits @jbonofre from the karaf community

I just repaced <Private-Package>*</Private-Package> with <Export-Package></Export-Package>, to get:

<plugin>
    <groupId>org.apache.felix</groupId>
    <artifactId>maven-bundle-plugin</artifactId>
    <configuration>
        <instructions>
            <Export-Package></Export-Package>
        </instructions>
    </configuration>
</plugin>

and consequently the import of org.slf4j.impl in my bundles manifest.mf disappeared, leaving only an import to org.slf4j. This is provided by PAX Logging, which is installed by default on karaf.