2
votes

I have a very simple OSGI app that just listens on register/unregister and sends some message to stdout.

What I have in my app is a simple Spring initialized bean. I use the maven bundle plugin to bundle the jar and put it into felix. This is an exceprt of the pom:

<dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>3.1.0.RELEASE</version>
        <type>jar</type>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <version>2.2.0</version>
            <extensions>true</extensions>
            <configuration>
                <instructions>
                    <Export-Package>org.spring.*</Export-Package>
                    <Bundle-Activator>foo.bar.Activator</Bundle-Activator>
                </instructions>
            </configuration>
        </plugin>
    </plugins>
</build>

The project compiles just fine and can be deployed into Felix just fine, however, when the activator is called, it complains about missing dependency like that:

Unresolved constraint in bundle foo.bar.project [56]: Unable to resolve 56.0: missing requirement [56.0] osgi.wiring.package; (&(osgi.wiring.package=org.springframework.context)(version>=3.1.0)(!(version>=4.0.0)))

I tried changing this line:

org.spring.*

To this:

*

In which felix complains that android-dalvik is not present, so I am completely clueless.

Any idea how to include Spring in an OSGI project without using Spring DM?

EDIT: Here is what the generated manifest complains about (from web console)

Exported Packages

foo.bar.osgi foo.bar.service foobar.tracker

Imported Packages org.osgi.framework,version=[1.6,2) from org.apache.felix.framework (0) org.osgi.util.tracker,version=[1.5,2) from org.apache.felix.framework (0) ERROR: org.springframework.context,version=[3.1,4) -- Cannot be resolved ERROR: org.springframework.context.support,version=[3.1,4) -- Cannot be resolved

Manifest Headers Bnd-LastModified: 1339014840268

Build-Jdk: 1.6.0_32

Built-By: fooBar

Bundle-Activator: foo.bar.osgi.Activator

Bundle-ManifestVersion: 2

Bundle-Name: fooBar

Bundle-SymbolicName: foo.bar.OSGIProject

Bundle-Version: 0.20.0.SNAPSHOT

Created-By: Apache Maven Bundle Plugin

Export-Package: foo.bar.osgi.tracker; uses:="foo.bar.osgi.service, org.osgi.util.tracker, org.osgi.framework",

foo.bar.osgi; uses:="org.springframework.context.support, org.springframework.context, org.osgi.framework", foo.bar.osgi.service

Import-Package:

org.osgi.framework; version="[1.6, 2)", org.osgi.util.tracker; version="[1.5, 2)", org.springframework.context; version="[3.1, 4)", org.springframework.context.support; version="[3.1, 4)" Manifest-Version: 1.0 Tool: Bnd-1.15.0

This is when I changed in pom

<configuration>
    <instructions>
        <Include-Resource>src/main/resources/spring-beans.xml</Include-Resource>
        <Bundle-Activator>nl.synovite.scheduled.osgi.Activator</Bundle-Activator>
        <Import-Package>*</Import-Package>></instructions>
</configuration>
2
Can you post the generated MANIFEST.MF? And why are you exporting "org.spring"? Is that the package name of your code base?Jonathan W
I think org.spring.* is being exported as a way of re-packaging every spring dependency into the built bundle. If you must repackage spring in your bundle (which isn't really in the modularity spirit of OSGi, imo), <Private-Package> might be better than <Export-Package>. This at least avoids exposing all the spring internals.Holly Cummins
Private package is indeed a better choice in this case (actually as far as I know, privatePackage is discarded in manifest by most OSGI providers). However, private or no, spring is not packaged at all into the jar. So I went ahead and added all spring dependencies in felix as osgified artifacts.Nikola Yovchev
Private-package is, as you say, a build instruction rather than a runtime one. This avoids mixing build concerns with runtime behaviour. (See my answer below for why neither instruction packages spring.)Holly Cummins

2 Answers

1
votes

Well, I think your bundle is basically fine, but Felix seems unable to resolve the org.springframework packages, so you need to add some bundle that exports the org.springframework packages. I don't know spring well, but I guess you can download those bundles from springsource.

Maven must have found one too as it seemed to be able to compile it.

1
votes

You are doing

<Export-Package>org.spring.*</Export-Package>

but the package of interest is org.springframework.context. The export syntax is a pattern, not a full regex, so the dot in your export really means dot (i.e. a package delimiter), and not an arbitrary character.

<Export-Package>org.spring*</Export-Package>

should do what you want.