How to convert a maven jar into an OSGI bundle? - osgi-bundle

How to convert a maven jar into an OSGI bundle? For this jar file, it also has dependencies on other external jar files. I tried maven-bundle-plugin, but how to write Import-Package is a disaster for me...... Can anyone help me out?
More specifically, what I want to transform is ProvToolbox https://lucmoreau.github.io/ProvToolbox/, which has several prov packages. So I need to transform them one by one.

Adding import packages is easy using the UI from Eclipse - just open the manifest, and in the Dependencies tab, you can add them.
This explains how to transform a JAR into an OSGi bundle:
link

Here you have a perfect example to create a fat bundle with all transitiv dependecies.
https://github.com/apache/jackrabbit/blob/2.18/jackrabbit-bundle/pom.xml
Notice the following tag:
<Embed-Transitive>true</Embed-Transitive>

Related

maven-bundle-plugin include non existing dependencies

I have a stange problem with the "maven-bundle-plugin" in a Eclipse Plugin Project.
When I compile the project, i see on the Manifest.xml, on the "Import-Package" section a lot of java packages imports for a lot of packages that doesn't exists on my classpath, for Example:
Import-Package:
COM.newmonics.PercClassLoader,
android.os,
bitronix.tm,
bitronix.tm.jndi,
bitronix.tm.resource.common,
bitronix.tm.resource.jdbc,
bitronix.tm.resource.jms,
bsh,
com.arjuna.ats.arjuna.common,
com.arjuna.ats.arjuna.recovery,
com.arjuna.ats.internal.jdbc,
com.arjuna.ats.internal.jta.recovery.arjunacore,
com.arjuna.ats.jbossatx.jta,
All of this packages don't exists on my project or in my dependencies, but maven-bundle-plugin add it to the Manifest, and I have no idea why. Any idea or suggestion? Thanks.
The bundle plugin generates Import-Package for the packages that are referred to by your project. These are the packages that should be exported by other bundles, so that your bundle can use them runtime. My first guess is that your classes refer to these packages, so check your source code first.
If your project really doesn't refer to those packages, please check if your pom.xml has specified these packages in an <Import-Package> directive in the plugin configuration. Perhaps it was copy/pasted from another project?
It could also be that you have embedded dependencies in your bundle jar that refer to the packages. To find out, you can unpack your jar (e.g. using the rjar tool) and grep the class files recursively for e.g. bitronix/tm.

no osgi ready dependencies

Currently I'm working with osgi and karaf.
My problem is the no "osgi ready" dependencies , which means a jar that is not ready to be deployed as a bundle into karaf for example.
I tried two solutions in order to deal with this kind of problems :
I tried to to use "Embed-Dependency" which will include the jar
dependency with the project... I don't think this could be a solution
because when I try to embed the jar , it will ask me to include other
jars that the first jar depend on , and so on ..
I tried to convert the no "osgi ready" jars into bundles using bnd tool or from "Plug-in from Existing JAR Archive" from eclipse project.
And this led to the same result , each jar will call another jar that it depend on it..
I am not sure if I'm doing it the wrong way or what is the problem exactly.
Any tips how to deal with no osgi ready dependencies ?
The simplest way to start is to use the wrap: protocol to auto create a jar. Behind the scenes it uses bnd to create a bundle on the fly. Simply prepend wrap: to the mvn url of the jar.
When you try to install the jar using bundle:install -s wrap:mvn:... karaf will tell you which imported packages are missing. Install jars that provide these packages in the same way. The pom of the jar can give you a hint what is missing.
This can mean to install lots of jars if your initial jars has lots of dependencies.
Once you have a list of jars that are installable together you can either create a feature using wrap protocol or you can make bundles from the individual at build time.
In any case you should take a look are the servicemix bundles. It provides OSGi ready bundles for many libraries.

JBoss Fuse vs. standard Maven dependency

I'm just learning OSGi, JBoss Fuse (6.1) and Karaf. How can I use a standard (not bundle) Maven dependencies without changing them?
I have a simple Maven bundle project. It depends on some third party libraries. It uses them via its blueprint.xml . I understand if the manifest.mf marks packages in the Import-Package entry then there have to be installed bundle with Export-Package in its manifest.mf. In my case if I install these standard Maven projects they won't export the required packages. So I've got the "Unresolved constraint in bundle" error message during the installation. Is the Maven Bundle or Shade plugin able to solve this issue? Or if they can't which is the most elegant way to resolve the dependencies?
Somewhere I've read the Jboss Fuse is able to resolve dependencies from the Maven repository. Can I use this mechanism somehow?
Thank you!
This may be a bit late now, but you can do that using features.
<features>
<feature name="wrap_features" version="1.0">
<bundle>mvn:org.apache.commons/com.springsource.org.apache.commons.logging/1.1.1</bundle>
<bundle>wrap:mvn:org.jdbi/jdbi/2.70</bundle>
<bundle>wrap:mvn:com.microsoft/sqljdbc4/4.2</bundle>
<bundle>wrap:mvn:org.springframework/spring-web/4.2.4.RELEASE</bundle>
<bundle>wrap:mvn:org.springframework.security/spring-security-core/4.0.3.RELEASE</bundle>
<bundle>mvn:com.doi.ws/mssql-fragment/1.0.0</bundle>
<bundle>mvn:com.doi.ws/mssql-impl/1.0.0</bundle>
<bundle>mvn:com.doi.ws/doi-services/1.0</bundle>
</feature>
</features>
You install the features before deploying your project to the fuse server using the features command
Meanwhile I found an almost good solution: with wrap: namespace/prefix Fuse automatically generates a bundle during installation:
osgi:install -s wrap:mvn:cglib/cglib/2.2.2
But I still have to install each dependency manually. Is there any way to automatize the installation of dependencies?
Thx!
Use Bundle-ClassPath manifest header.
Make sure all your maven dependencies are available in the final jar file under a single directory called 'lib'.( This can be achieved using maven resources plugin that copies maven dependencies in any output directory).
Use maven-bundle-plugin and customize the bundle manifest Bundle-ClassPath attribute with its element. Add a path to lib folder so that all the plain vanilla jars are available as a part of bundle classpath.

adding external jar files to OSGI

I have created an OSGi bundle and I want to add two external jar files to it called mediasenseplatform.jar and tinyos.jar So I added this line into my manifest file:
Bundle-ClassPAth:.,mediasenseplatform.jar,tinyos.jar
but it doesn't work and I don't see these files in my bundle jar file. How can I solve this problem?
OSGi doesn't make jar files, it is a platform for deploying and running them. If you want to include these jars in your bunde (which is just another jar) then you have to put them in there yourself.
There are build tools like maven which can do this for you.
You may also want to take a look at this related question.

IntelliJ: Including jars in a jar artifact

Developing on the Mac with IntelliJ 9.0.2 Community Edition.
I have a program which depends on two library jars. I have figured out how to get IntelliJ to make me a jar of my source (with the Artifact tab), and even to include the two jars in it.
However, if I get a listing of the jar file it produces, it looks like this:
com/acme/MyClass1.class
com/acme/MyClass2.class
...
mylib1.jar
myLib2.jar
And, no surprises, if I double-click the jar file, it gets a NoClassDefFoundError the first time it tries to access a class in one or other library jar.
It seems I need to tell IntelliJ to "inline" the library jars -- but this menu option is always greyed out!
Does anyone have any idea how to get jars inlined in a jar artifact?
IDEA doesn't support it yet, you can use Ant integration to package your jar (either by unpacking all the jars into the temp folder and then packaging the project output plus this temp folder into the single jar or by using some Ant task like jarjar).
If you want this feature to appear in the future IDEA versions, please vote for the request.
Having the dependency JARs included in your JAR should allow yoru code to run successfully. You probably don't have the JARs on your classpath.
I will use Maven Assembly plugin. Its simple and will give you a neat little jar file..

Resources