目录
- 使用Idea OSGI插件开发
- 使用Maven OSGI插件开发
使用Idea OSGI插件开发
下载Equinox SDK
https://download.eclipse.org/equinox/
安装OSGi插件
File->Setting->Plugins
创建一个OSGi项目或模块
Use Library选中后点击Create创建,选择下载好的SDK压缩包即可:D:\tools\osgi\equinox-SDK-4.15.zip
配置完成后可以点击下面的Configure…按钮保存配置,下次创建OSGi模块时就会默认选择该配置
配置OSGI
File->Settings->Languages & Frameworks->OSGi
Manifest入口1
File->Project Structure->Facets
Manifest入口2
File->Project Structure->Modules->OSGi->Manifest Generation
编译OSGi模块
写代码
右击模块选择build模块
查看OSGi模块对应的Manifest文件
Manifest-Version: 1.0
Bnd-LastModified: 1585806910451
Bundle-ManifestVersion: 2
Bundle-Name: org.equinoxosgi.toast.dev.gps
Bundle-SymbolicName: org.equinoxosgi.toast.dev.gps
Bundle-Version: 1.0.0
Created-By: 1.8.0_131 (Oracle Corporation)
Export-Package: org.equinoxosgi.toast.dev.gps;version="1.0.0"
Require-Capability: osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=1.8))"
Tool: Bnd-4.2.0.201903051501
使用Maven OSGI插件开发
源码地址:https://github.com/GallantKong/equinoxosgi-toast
Activator
区别主要是每个模块都提供了Activator实现,Equinox的模块入口,可以理解为Main类
gps-pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>toast</artifactId>
<groupId>org.equinoxosgi</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>toast-gps</artifactId>
<packaging>bundle</packaging>
<dependencies>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.4.0</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-Name>Toast Gps</Bundle-Name>
<Bundle-Version>${project.version}</Bundle-Version>
<Bundle-SymbolicName>org.equinoxosgi.toast.dev.gps</Bundle-SymbolicName>
<Bundle-Activator>org.equinoxosgi.toast.dev.gps.Activator</Bundle-Activator>
<Export-Package>
org.equinoxosgi.toast.dev.gps;version="${project.version}"
</Export-Package>
<Import-Package>
org.osgi.framework;version="[1.5,5.0)"
</Import-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>
airbag-pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>toast</artifactId>
<groupId>org.equinoxosgi</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>toast-airbag</artifactId>
<packaging>bundle</packaging>
<dependencies>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.core</groupId>
<artifactId>org.eclipse.core.jobs</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.4.0</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-Name>Toast Airbag</Bundle-Name>
<Bundle-Version>${project.version}</Bundle-Version>
<Bundle-SymbolicName>org.equinoxosgi.toast.dev.airbag</Bundle-SymbolicName>
<Bundle-Activator>org.equinoxosgi.toast.dev.airbag.Activator</Bundle-Activator>
<Export-Package>
org.equinoxosgi.toast.dev.airbag;version="${project.version}"
</Export-Package>
<Import-Package>
org.osgi.framework,
org.eclipse.core.runtime.jobs,
org.eclipse.core.runtime;common=split;version="3.5.0"
</Import-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>
emergency-pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>toast</artifactId>
<groupId>org.equinoxosgi</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>toast-emergency</artifactId>
<packaging>bundle</packaging>
<dependencies>
<dependency>
<groupId>org.equinoxosgi</groupId>
<artifactId>toast-gps</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.equinoxosgi</groupId>
<artifactId>toast-airbag</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.4.0</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-Name>Toast Emergency</Bundle-Name>
<Bundle-Version>${project.version}</Bundle-Version>
<Bundle-SymbolicName>org.equinoxosgi.toast.client.emergency</Bundle-SymbolicName>
<Bundle-Activator>org.equinoxosgi.toast.client.emergency.Activator</Bundle-Activator>
<Import-Package>
org.equinoxosgi.toast.dev.airbag;version="${project.version}",
org.equinoxosgi.toast.dev.gps;version="${project.version}",
org.osgi.framework;version="[1.5,5.0)"
</Import-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>
执行OSGI
idea点击Add Configuration添加运行配置或者菜单栏选择run->run->edit configuration
总结
如果报错:Unresolved requirement: Import-Package:org.eclipse.core.jobs。直接查看jobs包中MANIFEST.MF中的导出包的名称核对一下是否一致,像我的就是因为配置不一致导致的报错