Java 如何从 jar 库创建 OSGi 包?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3594024/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 02:20:20  来源:igfitidea点击:

How to create OSGi bundle from jar library?

javaosgibundle

提问by EK.

How to create OSGi bundle from jar library?

如何从 jar 库创建 OSGi 包?

采纳答案by wwerner

In case you are using eclipse: There is a wizard for that.

如果您使用的是 eclipse:有一个向导。

It allows you to select a number of jar libraries and creates a plug-in project (i.e. OSGi bundle) including these jars.

它允许您选择多个 jar 库并创建一个包含这些 jar 的插件项目(即 OSGi 包)。

You can find it here:

你可以在这里找到它:

File -> New -> Other ... -> Plug-in from Existing jar Archives.

alt text

替代文字

回答by Peter Tillemans

In principle you just need to add OSGi metadata to the manifest

原则上,您只需要将 OSGi 元数据添加到清单中

There is a bundle creator for eclipse which gives a very practical way to add these entries which should be part of the Plugin Dev Toolkit.

Eclipse 有一个包创建器,它提供了一种非常实用的方法来添加这些条目,这些条目应该是插件开发工具包的一部分。

Here is an articledetailing the process and how to do it with the Bnd tool, maven and so forth.

这是一篇详细介绍该过程以及如何使用 Bnd 工具、maven 等进行操作的文章。

I personally like the pax toolsvery much. It is all command line based, but very practical. To create an OSGi bundle of an existing jar you can use bnd tool.

我个人非常喜欢pax 工具。它都是基于命令行的,但非常实用。要创建现有 jar 的 OSGi 包,您可以使用bnd 工具

回答by Abdel Raoof

First check out if you can find a osgi enabled version of your library from repositories

首先检查是否可以从存储库中找到支持 osgi 的库版本

  1. SpringSource http://www.springsource.com/repository
  2. Fusesource http://repo.fusesource.com/
  1. SpringSource http://www.springsource.com/repository
  2. Fusesource http://repo.fusesource.com/

If you don't find the OSGi enabled versions. You can go ahead with using the pax tool - PaxConstructor use the aQute's Bnd tool.

如果您没有找到支持 OSGi 的版本。您可以继续使用 pax 工具 - PaxConstruct或使用aQute 的 Bnd 工具

回答by Gunnar

The Eclipse Bundle Recipeproject provides a Maven based approach for adding OSGi meta data to JARs consumed from a Maven repository.

Eclipse的包配方项目为添加的OSGi元数据从Maven仓库所消耗的JAR一个Maven为基础的方法。

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.

它的核心是使用bnd 工具。这个工具就像一把瑞士军刀。它分析 jars 和类文件并正确计算包的导入和导出。您应该使用 bnd 自己转换专有 jars。它在 Maven Central 中可用

回答by Renato

Late arrival to the party:

迟到派对:

If you're using Gradle, you can add the jar as a normal dependency of your project if you apply the osgi-runplugin.

如果您使用Gradle,并且应用osgi-run插件,则可以将 jar 添加为项目的正常依赖项。

The osgi-run plugin will wrap the jar transparently into a bundle for you, exporting every package in it and calculating all its imports. As Gradle will know the jar's transitive dependencies, it will do the same for them as well, if necessary.

osgi-run 插件将为您透明地将 jar 包装成一个包,导出其中的每个包并计算其所有导入。由于 Gradle 会知道 jar 的传递依赖项,因此如有必要,它也会对它们执行相同的操作。

The jar(s) will be part of the OSGi runtime osgi-run creates, which you can then start up with gradle runOsgior gradle createOsgi, then executing either the run.shor run.batscripts.

jar 将成为 osgi-run 创建的 OSGi 运行时的一部分,然后您可以使用gradle runOsgi或启动它gradle createOsgi,然后执行run.shrun.bat脚本。

The actual wrapping is done by Bnd, the swiss knife of the OSGi world, of course.

当然,实际的包装是由OSGi 世界的瑞士刀Bnd完成的。

If you want to configure how the wrapping happens (what should be imported/exported, usually), you can do it easily in the Gradle build file, see the documentationfor details.

如果您想配置包装如何发生(通常应该导入/导出什么),您可以在 Gradle 构建文件中轻松完成,有关详细信息,请参阅文档

Example:

例子:

wrapInstructions {
    // use regex to match file name of dependency
    manifest( "c3p0.*" ) {
        // import everything except the log4j package - should not be needed
        instruction 'Import-Package', '!org.apache.log4j', '*'
        instruction 'Bundle-Description', 'c3p0 is an easy-to-use library for making traditional ' +
                'JDBC drivers "enterprise-ready" by augmenting them with functionality defined by ' +
                'the jdbc3 spec and the optional extensions to jdbc2.'
    }
}

回答by Tarang Bhalodia

  • Create a new Plug-in project from existing JAR archive.
  • 从现有的 JAR 档案创建一个新的插件项目。

enter image description here

在此处输入图片说明

  • Add the jar file to be exported
  • 添加要导出的jar文件

enter image description here

在此处输入图片说明

  • Click next and name the project.
  • 单击下一步并命名项目。

enter image description here

在此处输入图片说明

  • NOTE:
  • Make sure OSGI framework is selected in target platform.
  • Unzip the JAR archives into the projectis deselected-> deselecting it, will export all the packages of the JAR

  • if Unzip the JAR archives into the projectis selectedthen you will manually need to export required packages in the MANIFEST.MFfile.

  • 笔记:
  • 确保在目标平台中选择了 OSGI 框架。
  • 将 JAR 压缩包解压到项目取消选择-> 取消选择,将导出 JAR 的所有包

  • 如果解压缩JAR包到项目选择,那么你将需要手动出口所需的软件包中的MANIFEST.MF文件。

Click finish. You will find project with name transport-5.1.1 created in your workspace. Also you can verify, all the packages of the JAR are exported in MANIFEST.MF file.

单击完成。您将在工作区中找到名为 transport-5.1.1 的项目。您也可以验证,JAR 的所有包都导出到 MANIFEST.MF 文件中。