OSGI 环境搭建

OSGI 环境搭建

OSGI Windows运行环境搭建(Linux也可参考该方式、只是文件路径换成Linux路径即可)。

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

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

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

为了方便使用,可以再创建一个 start.bat :
d:\osgi\equinox> vi start.bat
内容如下:
@echo on
java -Dosgi.console -jar org.eclipse.osgi_3.18.600.v20231110-1900.jar

后续就可以通过执行 start.bat 启动了:

d:\osgi\equinox> start.bat

d:\osgi\equinox>java -Dosgi.console -jar org.eclipse.osgi_3.18.600.v20231110-1900.jar
____________________________
Welcome to Apache Felix Gogo

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!  

最终发现启动必须的其实只有下面四个bundle,osgi.bundles 只需要指定下面几个即可:
file:./plugins/org.apache.felix.gogo.command_1.1.2.jar@2:start,
file:./plugins/org.apache.felix.gogo.runtime_1.1.6.jar@2:start,
file:./plugins/org.apache.felix.gogo.shell_1.1.4.jar@2:start,
file:./plugins/org.eclipse.equinox.console_1.4.600.v20231106-0859.jar@2:start

对应的 config.ini 配置文件的内容为:
osgi.bundles=file:./plugins/org.apache.felix.gogo.command_1.1.2.jar@2:start,file:./plugins/org.apache.felix.gogo.runtime_1.1.6.jar@2:start,file:./plugins/org.apache.felix.gogo.shell_1.1.4.jar@2:start,file:./plugins/org.eclipse.equinox.console_1.4.600.v20231106-0859.jar@2:start

另外这两个其实也没有start:

28      INSTALLED   org.eclipse.core.runtime_3.30.0.v20231102-0719
29      INSTALLED   org.eclipse.equinox.common_3.18.200.v20231106-1826


D:\osgi\equinox>start.bat

D:\osgi\equinox>java -Dosgi.console -jar org.eclipse.osgi_3.18.600.v20231110-1900.jar
____________________________
Welcome to Apache Felix Gogo

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
84      ACTIVE      org.apache.felix.gogo.command_1.1.2
85      ACTIVE      org.apache.felix.gogo.runtime_1.1.6
86      ACTIVE      org.apache.felix.gogo.shell_1.1.4
g! install my_bundles/com.xxx.osgi.hello.jar
Bundle ID: 87
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
84      ACTIVE      org.apache.felix.gogo.command_1.1.2
85      ACTIVE      org.apache.felix.gogo.runtime_1.1.6
86      ACTIVE      org.apache.felix.gogo.shell_1.1.4
87      INSTALLED   com.xxx.osgi.hello_1.0.0
g! start 87
hello world: start!

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
84      ACTIVE      org.apache.felix.gogo.command_1.1.2
85      ACTIVE      org.apache.felix.gogo.runtime_1.1.6
86      ACTIVE      org.apache.felix.gogo.shell_1.1.4
87      ACTIVE      com.xxx.osgi.hello_1.0.0
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>

再创建一个目录用于后续OSGI Frameworks配置时作为默认的output目录(非必须的步骤):
d:\osgi> mkdir osgi_def_output_bundles
d:\osgi> ls
equinox                  equinox-SDK-4.30         osgi_def_output_bundles

3,配置IDEA OSGI:
从 File-> Settings -> Languages & Frameworks 进入 OSGI 配置页面,
或者从Customize->All settings-> Languages & Frameworks 进入全局配置的 OSGI 配置页面:
OSGI 环境搭建_第1张图片

OSGI -> Framework Instances:添加 Equinox OSGi Framework:

OSGI 环境搭建_第2张图片

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

OSGI 环境搭建_第3张图片

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

OSGI 环境搭建_第4张图片

配置正确后、确认添加:

OSGI 环境搭建_第5张图片

4,继续进行 OSGI Project Defaults 配置:
OSGI Framework:选择上面OSGI配置界面刚刚添加的 OSGi framework:Equinox;
Default manifest:默认;
Output path:
如果是从项目 File->Settings 菜单进入的配置界面则使用默认的路径(默认为:项目根路径/项目名/out/bundles);
如果是从全局配置进入的配置界面则此默认显示为全局默认配置为用户tmp目录,将全局配置调整设置为前面创建的OSGI项目默认output路径:d:/osgi/osgi_def_output_bundles:

OSGI 环境搭建_第6张图片

或者:

OSGI 环境搭建_第7张图片

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

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

OSGI 环境搭建_第8张图片

选择:Use Library  & Create:

OSGI 环境搭建_第9张图片

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

OSGI 环境搭建_第10张图片

OSGI 环境搭建_第11张图片

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

OSGI 环境搭建_第12张图片

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

OSGI 环境搭建_第13张图片

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

META-INF\MANIFEST.MF

MANIFEST.MF 文件存储的实际上是 Bundle 的元数据,可以定义 Bundle 的各种特征属性。
MANIFEST.MF的文件格式:
1、属性声明一般为:name:value;
2、换行缩进行首一个空格;
3、每个子句(clause)进一步分解为一个目标(target)和一组由分号分隔的name-value参数(parameter)对;
4、文件内容最后两个换行(无空格);

例如:

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: hello
Bundle-SymbolicName: com.xxx.osgi.hello;singleton:=true
Bundle-Version: 1.0.0
Bundle-Activator: com.xxx.osgi.hello.Activator
Import-Package: org.osgi.framework
Export-Package: com.xxx.osgi.hello;uses:="org.osgi.framework";version=
 
"1.0.0"

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

OSGI 环境搭建_第14张图片

创建 Activator 以后,这里会提示 Bundle activator is not registered in manifest。还需要我们在 File->Project Structure->Modules 设置里面设置一下。
可以在General中设置Manifest文件位置,也可以在 Manifest Generation 中设置 Bundele Settings 参数,设置完以后,重新执行 Build(Ctrl+Shift+F9),然后可以通过 Tools->View Generated Manifest 查看生成并打包到 Jar 中的 Manifest。
一般来说应该会把 Manifest 文件放在项目里编辑并指定使用对应的Manifest文件,然后自动打包的时候会打包到 Jar 包中去。

OSGI 环境搭建_第15张图片

如果想要生成的 bundle jar 包带上版本号信息,可以修改下面这个配置加上版本号:

OSGI 环境搭建_第16张图片

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

OSGI 环境搭建_第17张图片

OSGI 环境搭建_第18张图片

IDEA运行/调试:
输入 ss  可查看OSGI运行状态:

OSGI 环境搭建_第19张图片

你可能感兴趣的:(开发,java,osgi)