Problems with maven built by OSGi, including dependencies

I am currently starting with OSGi, iPOJO and iPOJO Annotations and trying to create a simple component to deploy to Felix. Unfortunately, I encounter various problems that take me an hour to solve, or which I cannot solve even after I have spent a lot of time, for example:

I want to use an existing library in my OSGi suite that we create using Maven. The library is currently not an "OSGI-ified", and we do not plan to do this in the medium term. Because of this, I want to include this library and all its dependencies in a bundle using ...:

<Embed-Dependency>*</Embed-Dependency> <Embed-Transitive>true</Embed-Transitive> 

Now I have the following pom.xml file for the OSGi component:

 <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>foo</groupId> <artifactId>samplecomponent</artifactId> <packaging>bundle</packaging> <version>0.0.1-SNAPSHOT</version> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <compilerArgument>-Xlint:all</compilerArgument> <showWarnings>true</showWarnings> <source>1.6</source> <target>1.6</target> <compilerArguments> <encoding>UTF-8</encoding> </compilerArguments> <showDeprecation>true</showDeprecation> <verbose>true</verbose> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <extensions>true</extensions> <version>2.3.6</version> <configuration> <instructions> <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName> <Embed-Dependency>*</Embed-Dependency> <Embed-Transitive>true</Embed-Transitive> <Embed-Directory>lib</Embed-Directory> <Export-Package>*</Export-Package> <_exportcontents>*</_exportcontents> </instructions> </configuration> </plugin> <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-ipojo-plugin</artifactId> <version>1.6.0</version> <executions> <execution> <goals> <goal>ipojo-bundle</goal> </goals> </execution> </executions> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.apache.felix</groupId> <artifactId>org.apache.felix.ipojo.annotations</artifactId> <version>1.8.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>foo</groupId> <artifactId>mylibrary</artifactId> <version>1.2.3</version> <scope>compile</scope> </dependency> </dependencies> </project> 

The jar file of the build file was built without any problems, but when I deploy and run the package on Apache Felix, I get the following error:

 g! install file:/…/samplecomponent-0.0.1-SNAPSHOT.jar Bundle ID: 8 g! start 8 org.osgi.framework.BundleException: Unresolved constraint in bundle samplecomponent [8]: Unable to resolve 8.0: missing requirement [8.0] osgi.wiring.package; (osgi.wiring.package=com.sun.jdmk.comm) 

I set the journal level to the highest verbosity, unfortunately, more information. When I uninstall mylibrary, the package starts without problems.

Any suggestions appreciated!

+7
source share
2 answers

Apparently, the library uses com.sun.jdmk.comm , which is not displayed from the framework package. You can check this question if you really need it, or exclude it from import by adding additional instructions,

 <Import-Package>!com.sun.jdmk.comm, *</Import-Package> 
+9
source

If you have a huge list of exceptions, you should probably take this as an indication that something is wrong with your build process and package.

If it were me, the first thing I did was hack your package and see what classes it included and what packages it exported. I suspect you have a rather massive package, which means that your dependencies in the bundle will be quite extensive. This means that you are losing the many benefits of OSGi modularity, and this can cause many practical problems.

When declaring a package as optional, you say: "I am happy to accept ClassDefNotFoundExceptions for the classes in this package." For your code, you can probably find out if the package is really optional, but it's pretty hard to guess which packages used by a third-party user are optional. This, of course, is that pre-packaged cans are more convenient, but I understand that this does not help you much. :)

What you do by embedding a third-party library links it, but not so much to reuse it. (Another difference is that it will share the classloader with the implementation package, which can make it work much better if, for example, a third-party library tries to load your classified one by reflection.) Since you are having problems with dependencies, I would tends to take the hit of adding a build step that wraps a third-party jar so you can clearly see which classes and dependencies apply to your code and which additional jar.

Another thing I would look at is your export package = *. This will export each package to your class path, which means that all of these packages are built into your jar. And you get all your import packages. Your bad package becomes the whole application, not the lean OSGi module you were hoping for. You should limit your export to a minimum (you want your insides to be private, and you definitely don't want to share all the rest of the insides). A.

-

OSGi Enterprise in action: http://www.manning.com/cummins

+4
source

All Articles