Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
319 views
in Technique[技术] by (71.8m points)

java - Convert existing JAR to OSGi-bundle

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...