idea社区版开发OSGi项目

目录

  1. 使用Idea OSGI插件开发
  2. 使用Maven OSGI插件开发

使用Idea OSGI插件开发

下载Equinox SDK

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

安装OSGi插件

File->Setting->Plugins
1

创建一个OSGi项目或模块

Use Library选中后点击Create创建,选择下载好的SDK压缩包即可:D:\tools\osgi\equinox-SDK-4.15.zip
配置完成后可以点击下面的Configure…按钮保存配置,下次创建OSGi模块时就会默认选择该配置
2

配置OSGI

File->Settings->Languages & Frameworks->OSGi

3

Manifest入口1

File->Project Structure->Facets
4

Manifest入口2

File->Project Structure->Modules->OSGi->Manifest Generation
5

编译OSGi模块

写代码

右击模块选择build模块

6

查看OSGi模块对应的Manifest文件

7

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
8

总结

如果报错:Unresolved requirement: Import-Package:org.eclipse.core.jobs。直接查看jobs包中MANIFEST.MF中的导出包的名称核对一下是否一致,像我的就是因为配置不一致导致的报错
9

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值