Java 如何使用 eclipse 和 bndtools 将 jar 转换为 OSGi 包
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9819090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert jar to OSGi bundle using eclipse and bndtools
提问by Joly
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.
我正在寻找使用 eclipse bndtools 插件将 jar 转换为 OSGi 包的分步指南。我知道可以使用命令行使用 bnd 来完成它,但很高兴知道如何通过 IDE 执行相同的操作。
I might be missing something but this tutorialonly explains how to create a project from scratch.
我可能遗漏了一些东西,但本教程仅解释了如何从头开始创建项目。
采纳答案by Kuldeep Jain
Follow the article Create Eclipse plugins (OSGi bundles) from standard jarto achieve this. Though this approach does not use Bnd, but you would be able to achieve what you want.
按照从标准 jar 创建 Eclipse 插件(OSGi 包)一文来实现这一点。虽然这种方法不使用 Bnd,但您将能够实现您想要的。
In short you can do following:
简而言之,您可以执行以下操作:
Create a new Plugin project by selection
File-> New -> Project...-> Plug-in Development -> "Plug-in from Existing JAR Archives"
Select jars you want to have in this new plugin(bundle). Enter other plugin data(name, version, id etc.).
- Uncheck the flag
Unzip the JAR archive into the project
. Press then finish.
通过选择创建一个新的插件项目
File-> New -> Project...-> Plug-in Development -> "Plug-in from Existing JAR Archives"
在这个新插件(包)中选择你想要的 jars。输入其他插件数据(名称、版本、ID 等)。
- 取消选中标志
Unzip the JAR archive into the project
。按然后完成。
Unchecking the checkbox Unzip the JAR archive into the project
, prevents extracting class files from the Jar which is usually not necessary.
取消选中复选框Unzip the JAR archive into the project
,可防止从 Jar 中提取类文件,这通常是不必要的。
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"
.
编辑:导出您的包以将其安装到 OSGi 运行时中。选择您的捆绑包并选择File -> Export -> Plug-in Development -> "Deployable plug-ins and fragment"
。
Uncheck the checkbox to Export source.
取消选中导出源的复选框。
回答by Peter Kriens
- Just create a new project in bndtools for all (or related) jars that you want to convert.
- 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'
- Download the jar and sources in a jar directory
- Create a new bundle descriptor with a -classpath entry (File/New/Bundle Descriptor), for example:
- 只需在 bndtools 中为要转换的所有(或相关)jar 创建一个新项目。
- 为该项目指定一个名称,该名称将作为已转换 jar 的包符号名称的前缀。例如,如果您的公司是 acme,请将该项目称为“com.acme”
- 在 jar 目录中下载 jar 和源
- 使用 -classpath 条目(文件/新建/捆绑描述符)创建一个新的捆绑描述符,例如:
-classpath: jar/htmlcleaner-2.2.jar, jar/htmlcleaner-2.2-src.zip
-classpath: jar/htmlcleaner-2.2.jar, jar/htmlcleaner-2.2-src.zip
Export-Package: org.htmlcleaner.*;version=1.0
Export-Package: org.htmlcleaner.*;version=1.0
Import-Package: org.apache.tools.ant;resolution:=optional,\
Import-Package: org.apache.tools.ant;resolution:=optional,\
org.jdom;resolution:=optional,\
org.jdom;resolution:=optional,\
*
*
Bundle-Version: 2.2.1
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.
然后,您可以将捆绑包发布到存储库之一。选择 bnd.bnd 文件并使用上下文菜单选择 Release Bundle。
Edit: NBYou 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:
编辑:注意您不能直接使用来自其他项目的“包装”项目,因为 Eclipse 需要源代码树才能工作。有两种解决方法:
- 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
- 将打包的捆绑在存储库中并从那里使用它(如上所述)
- 解压项目src文件夹下的源码树
https://github.com/bndtools/bndtools/wiki/How-to-Wrap-Bundles
https://github.com/bndtools/bndtools/wiki/How-to-Wrap-Bundles
回答by evandor
Additionally to the existing answers you might be interested in this blog postwhich 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 ;))
除了现有的答案,您可能对这篇博文感兴趣,该博文解释了如何使用“纯 maven”从 jar 中创建 OSGi 包,即不需要特定的 IDE 和/或插件。(准确地说,在幕后,它再次与真正的工作一起工作;))