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.
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.
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.
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
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.