Convert existing JAR to OSGi-bundle
Asked Answered
L

4

11

I have a JAR file that I need to convert to an OSGi bundle. I do not have the original source code for the JAR file.

I tried to use the answers from: How to create OSGi bundle from jar library?

However, they are pretty outdated.

Edit: I need to convert several, but a fixed number of jars.

Luftwaffe answered 7/4, 2015 at 14:48 Comment(3)
Is there a method that does not involve Eclipse?Luftwaffe
wiki.osgi.org/wiki/MigrationSigma
This blog is about creating the bundle without involving Eclipse.Liuka
P
9

Option 1 - use bnd-platform to build your OSGi bundles when expecting frequent additions/updates of Jars, or when you can retrieve your dependencies from Maven repositories

We use bnd-platform (I am also the author) to manage third party dependencies and create OSGi bundles from them. You can use it with both dependencies retrieved from Maven repositories and local Jars (see the README). If you regularly add or update your dependencies I would suggest that you try bnd-platform. It's a plugin for Gradle, you can easily start with this template - just add your Jar files and provide the configuration as described in the project README (bundle symbolic names, versions) and run gradlew bundles.

Option 2 - use bnd to build your OSGi bundles when you do it once or additions/updates are seldom

If you only do this process once or seldom, a simple way to create an OSGi bundle from an existing Jar is to directly use bnd on the command line. The only thing you need is Java and the bnd jar. You can use wrap to try to automatically wrap a jar or create a .bnd file with instructions for bnd (e.g. to only export specific packages).

Example .bnd file:

-classpath: lib/trove-2.0.4.jar
-output: gnu.trove-2.0.4.jar
Export-Package: *;-split-package:=merge-last;-noimport:=true
Import-Package: *
Bundle-Version: 2.0.4
Bundle-Name: GNU Trove Collections Plug-in
Bundle-SymbolicName: gnu.trove

Example call:

java -jar <path to bnd>.jar trove-2.0.4.bnd

The bnd Jar download is no longer offered directly through the web site, a good alternative is to download it from Maven Central.

Propagandize answered 8/4, 2015 at 8:38 Comment(1)
Just adding this piece of information - you have to add the main jar with all its dependencies (the list separated by comma) in the -classpath entry. Correct me if I'm wrong.Denise
T
2

The Eclipse Bundle Recipe project provides a Maven based approach for adding OSGi meta data to JARs consumed from a Maven repository. Despite the name it doesn't use Eclipse.

At its core, it uses the bnd tool. This tool is like a swiss army knife. It analyzes jars and class files and properly calculate package import and exports. You should use bnd for converting proprietary jars yourself. It's available in Maven Central.

Telephone answered 8/4, 2015 at 11:50 Comment(2)
this won't work if you share this dependency across bundles - you will get a classloader conflictAugie
@zacheusz, Please open a new question with the specific problem you are facing. It looks like you are running into an issue with the specific library. In general, bundles created by processing JARs with bnd work very well. However, some libraries require additional steps to become good OSGi citizens.Telephone
J
2

If you're using Maven then you can use the Maven Bundle Plugin to inline or embed dependencies in a OSGi bundle:

http://felix.apache.org/documentation/subprojects/apache-felix-maven-bundle-plugin-bnd.html#embedding-dependencies

Jeanelle answered 8/4, 2015 at 12:10 Comment(1)
Hi, I wonder what if the jar also has dpenendencies of jar file? I tried maven-bundle-plugin to transform jar A to a bundle. However, when I check the MANIFEST.MF of the generated bundle, I saw in the Import-Package, there are a lot of jar packages which needs to be imported. I think this is a loop. I need to first transfrom jar A to a bundle, then I need to transform its jar dependencies to bundles, then dependencies of dependencies...... As long as there is a jar package in the Import-Package, I can not use bndrun to test this bundle......Matelote
D
0

or just use osgi:install with wrap option as below.

osgi:install wrap:file:/u01/repository/com/oracle/ojdbc6/11.2.0/ojdbc6-11.2.0.jar

this will deploy the jar file as bundle and you can get the bundle under "$fuse_home/data/cache/bundle{id}/version0.0" folder.

Dutyfree answered 1/2, 2016 at 17:0 Comment(1)
Just as a side note: To make this work you need to install pax url wrap.Lurette

© 2022 - 2024 — McMap. All rights reserved.