IanWatson IanWatson - 2 months ago 13
Java Question

OSGi (Karaf) Resolution vs Maven dependencies

In my example below there seems to be a discrepancy / duplication in the required steps in OSGi resolution and Maven dependency support.

I have a jar which is dependent on a external 3rd party jar, in this case time4j.

<dependency>
<groupId>net.time4j</groupId>
<artifactId>time4j-core</artifactId>
<version>4.16</version>
</dependency>


I can then run my simple jar locally by importing the packages and running etc.

When importing into OSGi I have to ensure that this jar is imported first, often using the PAX Wrap Url.

This is ok for 1 jar dependency but what about when there are multiple dependencies. I could use a features.xml file to collect these jars but why? I've already specified them in my pom.xml.

It seems there should be a way for OSGi / Karaf to read the pom.xml dependencies and import these into the container using the PAX Wrap url when needed.

Have I missed something here?

Answer

Sorry but your expectations are not in sync with reality. First of all Maven dependencies are build-time dependencies. That's why you declare dependencies you know to be available in the runtime as provided

<scope>provided</scope>

Neither OSGi nor Karaf can do anything about your build time dependencies. BUT with OSGi you can make sure your build dependencies are also available in your runtime and don't interfere with other libraries that might be available.

That's why you need to declare your imports and exports etc.

Karaf does help you with some of the dependencies for example with feature files. If you have a feature definition maven project, all of your compile scope dependencies can be included in one feature file.

BUT, the OSGi resolver only looks at the currently available bundles and nothing more, no connection what so ever to maven, if you want to have some sort of automagic resolving of external dependencies you need to make sure that you have
a) an OBR resolver enabled (this depends on the karaf version you are using, with 4.x it's already included) and
b) an OBR repository at hand, Karaf Cave would be the project to look for in that case, cause it can reside like a proxy on top of a maven repository.