OSGI 环境搭建

OSGI 环境搭建

OSGI Equinox Downloads 软件包官方下载页面:

https://download.eclipse.org/equinox/

里面有所有版本的下载连接。

我下载4.30版本下载:

https://archive.eclipse.org/equinox/drops/R-4.30-202312010110/index.php

Equinox Release Build: 4.30

1.All of Equinox:

A complete set of all bundles and launchers produced by the Equinox project. This zip is also a p2 repo.

* equinox-SDK-4.30.zip :

https://archive.eclipse.org/equinox/drops/R-4.30-202312010110/download.php?dropFile=equinox-SDK-4.30.zip

如果只是命令行使用可以不下载完整的SDK包(SDK包有20~30MB,相对大一些),如果直接下载所需要的jar包则比较小。

如果直接下载了完整的SDK包,那后面的依赖就不用再单独下载了,SDK解压以后都有了。

2.Framework Only:

The Equinox OSGi R4 framework implementation in a standalone package.

* org.eclipse.osgi_3.18.600.v20231110-1900.jar :

https://archive.eclipse.org/equinox/drops/R-4.30-202312010110/download.php?dropFile=org.eclipse.osgi_3.18.600.v20231110-1900.jar

3.Add-on Bundles:

Individual bundles that provide standalone OSGi specified services or add-on mechanisms (e.g., the Eclipse extension registry) of interest to OSGi programmers.

* org.eclipse.equinox.app_1.6.400.v20231103-0807.jar 92K

* org.eclipse.equinox.cm_1.5.300.v20231012-1248.jar 80K

* org.eclipse.equinox.common_3.18.200.v20231106-1826.jar 151K

* org.eclipse.equinox.device_1.1.500.v20231104-1104.jar 36K

* org.eclipse.equinox.event_1.6.300.v20231012-1251.jar 35K

* org.eclipse.equinox.console_1.4.600.v20231106-0859.jar 131K

* org.eclipse.equinox.launcher_1.6.600.v20231106-1826.jar 55K

* org.eclipse.equinox.jsp.jasper_1.1.800.v20231012-1238.jar 29K

* org.eclipse.equinox.jsp.jasper.registry_1.2.200.v20231012-1239.jar 14K

* org.eclipse.equinox.metatype_1.6.400.v20231013-0859.jar 100K

* org.eclipse.equinox.preferences_3.10.400.v20231102-2218.jar 137K

* org.eclipse.equinox.registry_3.11.400.v20231102-2218.jar 199K

* org.eclipse.osgi.services_3.11.200.v20231106-0901.jar 48K

* org.eclipse.equinox.supplement_1.10.700.v20230509-2241.jar 110K

* org.eclipse.equinox.transforms.xslt_1.2.400.v20231012-1247.jar 21K

* org.eclipse.equinox.transforms.hook_1.3.400.v20231012-1246.jar 32K

* org.eclipse.equinox.useradmin_1.2.500.v20231012-1249.jar 41K

* org.eclipse.osgi.util_3.7.300.v20231104-1118.jar 14K

* org.eclipse.equinox.servletbridge_1.6.400.v20231104-1108.jar 50K

* org.eclipse.equinox.http.servletbridge_1.2.300.v20231018-1317.jar 11K

* org.eclipse.equinox.http.jetty_3.9.0.v20231104-1114.jar 29K

* org.eclipse.equinox.http.registry_1.3.400.v20231012-1210.jar 50K

* org.eclipse.equinox.http.servletbridge_1.2.300.v20231018-1317.jar 11K

* org.eclipse.equinox.http.servletbridge_1.2.300.v20231018-1317.jar 11K

* org.eclipse.equinox.simpleconfigurator_1.5.0.v20230919-1457.jar 49K

* org.eclipse.equinox.concurrent_1.2.200.v20231103-0833.jar 28K

* org.eclipse.equinox.coordinator_1.5.200.v20230120-0604.jar 40K

* org.eclipse.equinox.region_1.5.400.v20231012-1234.jar 79K

4.Other Required Bundles:

A convenient set of bundles that are required by some of the Equinox bundles.

* org.apache.commons.logging_1.2.0.jar 60K

https://archive.eclipse.org/equinox/drops/R-4.30-202312010110/download.php?dropFile=org.apache.commons.logging_1.2.0.jar

* org.apache.felix.gogo.command_1.1.2.jar 39K

https://archive.eclipse.org/equinox/drops/R-4.30-202312010110/download.php?dropFile=org.apache.felix.gogo.command_1.1.2.jar

* org.apache.felix.gogo.runtime_1.1.6.jar 199K

https://archive.eclipse.org/equinox/drops/R-4.30-202312010110/download.php?dropFile=org.apache.felix.gogo.runtime_1.1.6.jar

* org.apache.felix.gogo.shell_1.1.4.jar 58K

https://archive.eclipse.org/equinox/drops/R-4.30-202312010110/download.php?dropFile=org.apache.felix.gogo.shell_1.1.4.jar

  1. Native Launchers

Platform-specific native launchers (e.g., eclipse.exe) for the Equinox framework. See the list of supported OS configurations.

* launchers-win32.win32.x86_64.4.30.zip 903K

* launchers-linux.gtk.x86_64.4.30.tar.gz 606K

* launchers-linux.gtk.ppc64le.4.30.tar.gz 609K

* launchers-linux.gtk.aarch64.4.30.tar.gz 605K

* launchers-macosx.cocoa.x86_64.4.30.tar.gz 542K

* launchers-macosx.cocoa.aarch64.4.30.tar.gz 581K

  1. OSGi starter kits

A useful collection of Equinox bundles packaged as a ready to run system. This include the framework, p2 and other frequently used service implementations.

* EclipseRT-OSGi-StarterKit-4.30-win32-win32-x86_64.zip 20M

* EclipseRT-OSGi-StarterKit-4.30-linux-gtk-x86_64.tar.gz 19M

* EclipseRT-OSGi-StarterKit-4.30-macosx-cocoa-x86_64.tar.gz 17M

创建软件包下载存储目录

d:> mkdir osgi_soft_download

下载软件包

创建运行目录

d:> mkdir osgi\equinox

d:> cd osgi\equinox

equinox下再创建两个目录:plugins、configuration

d:\osgi\equinox> mkdir configuration

d:\osgi\equinox> mkdir plugins

拷贝 equinox osgi framework jar 包到 d:\osgi\equinox 目录下

d:\osgi\equinox> cp d:\osgi_soft_download\all-of-equinox(sdk)\equinox-SDK-4.30\plugins\org.eclipse.osgi_3.18.600.v20231110-1900.jar .\

查看当前目录下的内容:

d:\osgi\equinox> ls -1

configuration

org.eclipse.osgi_3.18.600.v20231110-1900.jar

plugins

d:\osgi\equinox>

执行命令启动 osgi

d:\osgi\equinox> java -Dosgi.console -jar org.eclipse.osgi_3.18.600.v20231110-1900.jar

如果没有 osgi> 命令行界面,则查看 configuration 目录下的日志信息,提示未找到 org.eclipse.equinox.console bundle:

d:\osgi\equinox> cat configuration\1701746744550.log

!SESSION 2023-12-05 11:25:44.477 -----------------------------------------------

eclipse.buildId=unknown

java.version=1.8.0_40

java.vendor=Oracle Corporation

BootLoader constants: OS=win32, ARCH=x86_64, WS=win32, NL=zh_CN

!ENTRY org.eclipse.osgi 4 0 2023-12-05 11:25:44.820

!MESSAGE Could not find bundle: org.eclipse.equinox.console

!STACK 0

org.osgi.framework.BundleException: Could not find bundle: org.eclipse.equinox.console

at org.eclipse.core.runtime.internal.adaptor.ConsoleManager.checkForConsoleBundle(ConsoleManager.java:69)

at org.eclipse.core.runtime.adaptor.EclipseStarter.startup(EclipseStarter.java:352)

at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:251)

at org.eclipse.core.runtime.adaptor.EclipseStarter.main(EclipseStarter.java:228)

!ENTRY org.eclipse.osgi 4 0 2023-12-05 11:25:44.828

!MESSAGE Application error

!STACK 1

java.lang.IllegalStateException: Unable to acquire application service. Ensure that the org.eclipse.core.runtime bundle is resolved and started (see config.ini).

at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:81)

at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:402)

at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:255)

at org.eclipse.core.runtime.adaptor.EclipseStarter.main(EclipseStarter.java:228)

d:\osgi\equinox>

报错信息中提示有几个依赖的 bundle 没有找到、提示检查 config.ini 配置:

org.eclipse.equinox.console

org.eclipse.core.runtime

拷贝依赖的 bundle:

d:\osgi\equinox> cp d:\osgi_soft_download\all-of-equinox(sdk)\equinox-SDK-4.30\plugins\org.eclipse.equinox.console_1.4.600.v20231106-0859.jar .\plugins\

d:\osgi\equinox> cp d:\osgi_soft_download\all-of-equinox(sdk)\equinox-SDK-4.30\plugins\org.eclipse.core.runtime_3.30.0.v20231102-0719.jar .\plugins\

d:\osgi\equinox>ls plugins

org.eclipse.core.runtime_3.30.0.v20231102-0719.jar

org.eclipse.equinox.console_1.4.600.v20231106-0859.jar

d:\osgi\equinox>

在 d:\osgi\equinox\configuration\ 目录下创建 config.ini 文件,指定依赖所必须的 bundles,内容如下:

d:\osgi\equinox> vi configuration\config.ini

osgi.bundles=file:./plugins/org.eclipse.equinox.console_1.4.600.v20231106-0859.jar@start,file:./plugins/org.eclipse.core.runtime_3.30.0.v20231102-0719.jar@start

再重启动 osgi:

d:\osgi\equinox> java -Dosgi.console -jar org.eclipse.osgi_3.18.600.v20231110-1900.jar

还是有问题、没有出现osgi>命令行,检查日志、还是存在依赖的 bundles 未找到:

d:\osgi\equinox>cat configuration\1701747671509.log |grep "Unresolved requirement"

Unresolved requirement: Import-Package: org.apache.felix.service.command; version="[1.0.0,2.0.0)"

Unresolved requirement: Require-Bundle: org.eclipse.equinox.common; bundle-version="[3.18.0,4.0.0)"; visibility:="reexport"

Unresolved requirement: Import-Package: org.apache.felix.service.command; version="[1.0.0,2.0.0)"

Unresolved requirement: Import-Package: org.apache.felix.service.command; version="[1.0.0,2.0.0)"

Unresolved requirement: Import-Package: org.apache.felix.service.command; version="[1.0.0,2.0.0)"

Unresolved requirement: Require-Bundle: org.eclipse.equinox.common; bundle-version="[3.18.0,4.0.0)"; visibility:="reexport"

d:\osgi\equinox>

接下来把官方介绍的 required bundles 全部拷贝到 plugins 下并配置 config.ini:

拷贝依赖的 bundles & 配置 config.ini:

d:\osgi\equinox> cp d:\osgi_soft_download\all-of-equinox(sdk)\equinox-SDK-4.30\plugins\org.eclipse.equinox.common_3.18.200.v20231106-1826.jar .\plugins\

d:\osgi\equinox> cp d:\osgi_soft_download\all-of-equinox(sdk)\equinox-SDK-4.30\plugins\org.apache.felix.gogo.command_1.1.2.jar .\plugins\

d:\osgi\equinox> cp d:\osgi_soft_download\all-of-equinox(sdk)\equinox-SDK-4.30\plugins\org.apache.felix.gogo.runtime_1.1.6.jar .\plugins\

d:\osgi\equinox> cp d:\osgi_soft_download\all-of-equinox(sdk)\equinox-SDK-4.30\plugins\org.apache.felix.gogo.shell_1.1.4.jar .\plugins\

查看 plugins 目录下的 bundles:

d:\osgi\equinox>ls ./plugins

org.apache.felix.gogo.command_1.1.2.jar

org.apache.felix.gogo.runtime_1.1.6.jar

org.apache.felix.gogo.shell_1.1.4.jar

org.eclipse.core.runtime_3.30.0.v20231102-0719.jar

org.eclipse.equinox.common_3.18.200.v20231106-1826.jar

org.eclipse.equinox.console_1.4.600.v20231106-0859.jar

d:\osgi\equinox>

修改 config.ini 配置文件:

d:\osgi\equinox> vi configuration\config.ini

osgi.bundles=file:./plugins/org.eclipse.equinox.console_1.4.600.v20231106-0859.jar@start,file:./plugins/org.eclipse.core.runtime_3.30.0.v20231102-0719.jar@start,file:./plugins/org.eclipse.equinox.common_3.18.200.v20231106-1826.jar@start,file:./plugins/org.apache.felix.gogo.command_1.1.2.jar@start,file:./plugins/org.apache.felix.gogo.runtime_1.1.6.jar@start,file:./plugins/org.apache.felix.gogo.shell_1.1.4.jar@start

重新执行命令启动 osgi(启动成功了):

d:\osgi\equinox> vi configuration\config.ini

d:\osgi\equinox> java -Dosgi.console -jar org.eclipse.osgi_3.18.600.v20231110-1900.jar


Welcome to Apache Felix Gogo

g!

输入 ss 查看加载的 bundles 信息:

g! ss

"Framework is launched."

id State Bundle

0 ACTIVE org.eclipse.osgi_3.18.600.v20231110-1900

1 ACTIVE org.eclipse.equinox.console_1.4.600.v20231106-0859

2 INSTALLED org.eclipse.core.runtime_3.30.0.v20231102-0719

3 INSTALLED org.eclipse.equinox.common_3.18.200.v20231106-1826

4 ACTIVE org.apache.felix.gogo.command_1.1.2

5 ACTIVE org.apache.felix.gogo.runtime_1.1.6

6 ACTIVE org.apache.felix.gogo.shell_1.1.4

g!

如果要加载其他的 bundle,同样的也只需要将 jar 拷贝过来,然后 config.ini 配置文件中修改、或者直接执行的命令行上通过参数 -Dosgi.bundles 指定加载:

d:\osgi\equinox> java -Dosgi.console -Dosgi.bundles=./xxx.jar -jar org.eclipse.osgi_3.18.600.v20231110-1900.jar

加载SDK中的plugins下的其他bunder时,注意需要把依赖的bunder也要一起加载。

从运行结果可以看出,JVM参数 "-Dosgi.bundles=xxx.jar" 将对应组件部署到了 OSGI 运行环境中。如果显示该组件的状态为"RESOLVED",即表示该组件被框架解析但没有启动。

如果用户希望组件在加载的同时启动该组件,可以修改上述参数为:-Dosgi.bundles=./xxx.jar@2:start。

"@2" 指明该组件的启动级别,":"后的"start"标明该组件在加载后启动。

如果不指定启动级别、使用默认级别启动、则也可以配置为:"@start"。默认级别在 configuration\config.ini 中通过参数 osgi.bundles.defaultStartLevel=2 可以指定。

进入osgi命令行以后,可以执行相关管理命令进行 install / start / stop / uninstall 模块管理(help 可查看命令帮助):

g! ss

"Framework is launched."

id State Bundle

0 ACTIVE org.eclipse.osgi_3.18.600.v20231110-1900

27 ACTIVE org.eclipse.equinox.console_1.4.600.v20231106-0859

28 INSTALLED org.eclipse.core.runtime_3.30.0.v20231102-0719

29 INSTALLED org.eclipse.equinox.common_3.18.200.v20231106-1826

30 ACTIVE org.apache.felix.gogo.command_1.1.2

31 ACTIVE org.apache.felix.gogo.runtime_1.1.6

32 ACTIVE org.apache.felix.gogo.shell_1.1.4

g! ls

D:\osgi\equinox\configuration

D:\osgi\equinox\helloworld-1.0-SNAPSHOT-shaded-with-dependencies.jar

D:\osgi\equinox\org.eclipse.osgi_3.18.600.v20231110-1900.jar

D:\osgi\equinox\plugins

g! install helloworld-1.0-SNAPSHOT-shaded-with-dependencies.jar

Bundle ID: 36

g! ss

"Framework is launched."

id State Bundle

0 ACTIVE org.eclipse.osgi_3.18.600.v20231110-1900

27 ACTIVE org.eclipse.equinox.console_1.4.600.v20231106-0859

28 INSTALLED org.eclipse.core.runtime_3.30.0.v20231102-0719

29 INSTALLED org.eclipse.equinox.common_3.18.200.v20231106-1826

30 ACTIVE org.apache.felix.gogo.command_1.1.2

31 ACTIVE org.apache.felix.gogo.runtime_1.1.6

32 ACTIVE org.apache.felix.gogo.shell_1.1.4

36 INSTALLED unknown_0.0.0 [36]

g! lb

START LEVEL 6

ID|State |Level|Name

0|Active | 0|OSGi System Bundle (3.18.600.v20231110-1900)|3.18.600.v20231110-1900

27|Active | 2|Console plug-in (1.4.600.v20231106-0859)|1.4.600.v20231106-0859

28|Installed | 2|Core Runtime (3.30.0.v20231102-0719)|3.30.0.v20231102-0719

29|Installed | 2|Common Eclipse Runtime (3.18.200.v20231106-1826)|3.18.200.v20231106-1826

30|Active | 2|Apache Felix Gogo Command (1.1.2)|1.1.2

31|Active | 2|Apache Felix Gogo Runtime (1.1.6)|1.1.6

32|Active | 2|Apache Felix Gogo Shell (1.1.4)|1.1.4

36|Installed | 1|file:/D:/osgi/equinox/helloworld-1.0-SNAPSHOT-shaded-with-dependencies.jar (0.0.0)|0.0.0

g! start 36

g! lb

START LEVEL 6

ID|State |Level|Name

0|Active | 0|OSGi System Bundle (3.18.600.v20231110-1900)|3.18.600.v20231110-1900

27|Active | 2|Console plug-in (1.4.600.v20231106-0859)|1.4.600.v20231106-0859

28|Installed | 2|Core Runtime (3.30.0.v20231102-0719)|3.30.0.v20231102-0719

29|Installed | 2|Common Eclipse Runtime (3.18.200.v20231106-1826)|3.18.200.v20231106-1826

30|Active | 2|Apache Felix Gogo Command (1.1.2)|1.1.2

31|Active | 2|Apache Felix Gogo Runtime (1.1.6)|1.1.6

32|Active | 2|Apache Felix Gogo Shell (1.1.4)|1.1.4

36|Active | 1|file:/D:/osgi/equinox/helloworld-1.0-SNAPSHOT-shaded-with-dependencies.jar (0.0.0)|0.0.0

g! stop 36

g! ss

"Framework is launched."

id State Bundle

0 ACTIVE org.eclipse.osgi_3.18.600.v20231110-1900

27 ACTIVE org.eclipse.equinox.console_1.4.600.v20231106-0859

28 INSTALLED org.eclipse.core.runtime_3.30.0.v20231102-0719

29 INSTALLED org.eclipse.equinox.common_3.18.200.v20231106-1826

30 ACTIVE org.apache.felix.gogo.command_1.1.2

31 ACTIVE org.apache.felix.gogo.runtime_1.1.6

32 ACTIVE org.apache.felix.gogo.shell_1.1.4

36 RESOLVED unknown_0.0.0 [36]

g! uninstall 36

g! ss

"Framework is launched."

id State Bundle

0 ACTIVE org.eclipse.osgi_3.18.600.v20231110-1900

27 ACTIVE org.eclipse.equinox.console_1.4.600.v20231106-0859

28 INSTALLED org.eclipse.core.runtime_3.30.0.v20231102-0719

29 INSTALLED org.eclipse.equinox.common_3.18.200.v20231106-1826

30 ACTIVE org.apache.felix.gogo.command_1.1.2

31 ACTIVE org.apache.felix.gogo.runtime_1.1.6

32 ACTIVE org.apache.felix.gogo.shell_1.1.4

g!

IDEA Equinox OSGi Framework 配置:

1,下载Eclipse Equinox SDK包(equinox-SDK-4.30.zip),我之前已经下载过了。

2,后续 IDEA 配置 OSGI 时需要指定Equinox SDK目录,为了使用方便,我把SDK拷贝到d:\osgi 目录下:

d:> cd osgi

d:\osgi> cp -r d:/osgi_soft_download/all-of-equinox(sdk)/equinox-SDK-4.30 d:/osgi

D:\osgi>ls -1

equinox

equinox-SDK-4.30

D:\osgi>

3,配置IDEA OSGI:File-> Settings -> File -> Languages & Frameworks 配置OSGI:

指定D:\osgi\equinox目录作为Equinox OSGI Framework根目录,即:Home directory 指定为 D:\osgi\equinox。选择正确的Home directory以后,会自动识别出Name & Version:

注意:如果选择了错误的Home directory,会报错并提示IDEA未找到 Equinox framework system bundle:

配置正确后、确认添加:

4,配置IDEA OSGI:File-> Settings -> File -> Languages & Frameworks 配置OSGI Project Defaults:

选择上面配置的OSGi frame work:

import Bnd/Bndtools projects automatically :指的是将普通的jar包打包为OSGI 可用的jar。

IDEA 的 OSGI Framework 配置完成以后,File->Project->New Project创建新的项目时,就可以选择 JavaEE、指定创建OSGI项目了:

选择:Use Library & Create:

找到并选择SDK的plugins目录下的 org.eclipse.osgi_3.18.600.v20231110-1900.jar:

下一步、填写新建项目的属性:

IDEA 创建完成,但不会帮我们生成Activator以及Manifest.mf文件:

后续需要自己创建Activator以及Manifest.mf文件。

Activator:

这个会处理OSGI的start() 和 stop()时调用,相当于我们的main函数。如果用Eclipse创建项目时可以通过勾选选项自动生成,IDEA创建不会生成。

META-INF\MANIFEST.MF:

MANIFEST.MF文件存储的实际上是 Bundle 的元数据,可以定义 Bundle 的各种特征属性。

MANIFEST.MF的文件格式:

1、属性声明一般为:name:value;

2、换行缩进一个空格;

3、每个子句(clause)进一步分解为一个目标(target)和一组由分号分隔的name-value参数(parameter)对。

IDEA OSGI Console启动:

Run->Edit Configurations,点击 + ,选择OSGI Bundles,添加核心bundles:

IDEA运行/调试:

输入 ss 可查看OSGI运行状态:

相关推荐
救救孩子把16 分钟前
深入理解 Java 对象的内存布局
java
落落落sss18 分钟前
MybatisPlus
android·java·开发语言·spring·tomcat·rabbitmq·mybatis
万物皆字节24 分钟前
maven指定模块快速打包idea插件Quick Maven Package
java
夜雨翦春韭30 分钟前
【代码随想录Day30】贪心算法Part04
java·数据结构·算法·leetcode·贪心算法
我行我素,向往自由37 分钟前
速成java记录(上)
java·速成
一直学习永不止步43 分钟前
LeetCode题练习与总结:H 指数--274
java·数据结构·算法·leetcode·数组·排序·计数排序
邵泽明43 分钟前
面试知识储备-多线程
java·面试·职场和发展
程序员是干活的1 小时前
私家车开车回家过节会发生什么事情
java·开发语言·软件构建·1024程序员节
煸橙干儿~~1 小时前
分析JS Crash(进程崩溃)
java·前端·javascript
2401_854391081 小时前
Spring Boot大学生就业招聘系统的开发与部署
java·spring boot·后端