How to convert jar to OSGi bundle using eclipse and bndtools
Asked Answered
O

3

14

I am looking for a step by step guide to convert jar into an OSGi bundle using the eclipse bndtools plugin. I know it is possible to do it with bnd using the command line but would be nice to know how to do the same via the IDE.

I might be missing something but this tutorial only explains how to create a project from scratch.

Oller answered 22/3, 2012 at 9:4 Comment(0)
H
27

Follow the article Create Eclipse plugins (OSGi bundles) from standard jar to achieve this. Though this approach does not use Bnd, but you would be able to achieve what you want.

In short you can do following:

  1. Create a new Plugin project by selection File-> New -> Project...-> Plug-in Development -> "Plug-in from Existing JAR Archives"

  2. Select jars you want to have in this new plugin(bundle). Enter other plugin data(name, version, id etc.).

  3. Uncheck the flag Unzip the JAR archive into the project. Press then finish.

Unchecking the checkbox Unzip the JAR archive into the project, prevents extracting class files from the Jar which is usually not necessary.

EDIT : To Export your bundle to install it into a OSGi runtime. Select your bundle and choose File -> Export -> Plug-in Development -> "Deployable plug-ins and fragment".

Uncheck the checkbox to Export source.

Hypogene answered 22/3, 2012 at 11:6 Comment(7)
One should note that although this does exactly what the OP wanted to accomplish, it does not require the BND plugin. This is standard eclipse.Harragan
Thanks. Now that I've created the project with my importet jar how do I export it as an OSGi bundle?Oller
Open the file "MANIFEST.MF" and validate that all required package are exported on the tab "Runtime". Also read through "Using the new plugin project" in the article that I mentioned.Hypogene
Everything is there indeed but how do I export it as a jar now as I need to install it on another OSGi container not related to Eclipse?Oller
Bear in mind that because this approach does not use Bnd, you will have to manually add the Import-Package statement for the bundle that you are wrapping. Until you do this, the bundle is likely to crash at runtime with NoClassDefFoundErrors etc. Bndtools does have equivalent functionality with the advantage of also generating dependencies via bnd... unfortunately I haven't got around to documenting this yet.Tramontane
Downvoter: Please care to explain your down-vote. I need to know if something is wrong with my answer.Hypogene
@KuldeepJain I already did explain above. Also: your answer doesn't actually answer the question as asked.Tramontane
T
11
  1. Just create a new project in bndtools for all (or related) jars that you want to convert.
  2. Give this project a name that will be the prefix of the bundle symbolic name of the converted jars. E.g. if your company is acme, call the project 'com.acme'
  3. Download the jar and sources in a jar directory
  4. Create a new bundle descriptor with a -classpath entry (File/New/Bundle Descriptor), for example:

-classpath: jar/htmlcleaner-2.2.jar, jar/htmlcleaner-2.2-src.zip

Export-Package: org.htmlcleaner.*;version=1.0

Import-Package: org.apache.tools.ant;resolution:=optional,\

org.jdom;resolution:=optional,\

*

Bundle-Version: 2.2.1

After saving this file, look in the generated directory, voila, there is your bundle! You can reuse the same project for any number of bundles you want to wrap.

You can then release the bundle to one of the repositories. Select the bnd.bnd file and select Release Bundle with the context menu.


Edit: NB You can't directly use a 'wrap' project from other projects, since Eclipse needs the source tree for that to work. There are 2 workarounds for this:

  • Put the wrapped bundled in a repository and use it from there (as described above)
  • Unpack the source tree in the src folder of the project

https://github.com/bndtools/bndtools/wiki/How-to-Wrap-Bundles

Towards answered 23/3, 2012 at 10:30 Comment(1)
I updated your answer to include the caveat mentioned on the bndtools wiki.Moidore
E
0

Additionally to the existing answers you might be interested in this blog post which explains how to create that OSGi bundle from a jar with "pure maven", i.e. without the need of a specific IDE and/or plugins. (To be precise, under the hood it is once more bnd with does the real work ;))

Eby answered 16/1, 2014 at 14:6 Comment(2)
the blog post isn't available anymore.Singlefoot
Sorry, I deleted the whole blog a while ago and I do not have that post any more.Eby

© 2022 - 2024 — McMap. All rights reserved.