Integrate non-OSGi Dependencies
Consider a scenario where you need to use a non-OSGi jar in your OSGi project. Most libraries are not OSGi bundles and we should repackage all them with OGSi headers to be loaded into the OSGi container. There are several ways to help you with these non-OSGi dependencies:
For instance, you need to utilize fastjson library to serialize/deserialize json object in your OSGi bundle.
Embed these jar files into your OSGi bundle
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.4.0</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
<Bundle-Version>${project.version}</Bundle-Version>
<Bundle-Activator>com.example.demo.Activator</Bundle-Activator>
<Export-Package>
com.example.demo*;version=${project.version}
</Export-Package>
<Import-Package>
!org.springframework.*,
*
</Import-Package>
<Embed-Dependency>fastjson;scope=compile|runtime|system;inline=true</Embed-Dependency>
</instructions>
</configuration>
</plugin>
If you want a dependency inlined instead of embedded add the inline=true.
Wrap these jar files within one OSGi bundle
Karaf supports the wrap:protocol execution, so it allows for directly deploying third party dependencies into OSGi container:
install -s wrap:mvn:com.alibaba/fastjson/1.1.37
You can also create a wrap bundle for a third party dependency. This bundle is simply a Maven POM that shades an existing jar and package into a jar bundle.
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.4.0</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
<Bundle-Version>${project.version}</Bundle-Version>
<Import-Package>!org.springframework.*,*</Import-Package>
<Private-Package>!*</Private-Package>
<Embed-Dependency>*;scope=compile;type=!pom;inline=true</Embed-Dependency>
<Export-Package>com.alibaba.fastjson*</Export-Package>
</instructions>
</configuration>
</plugin>
References
http://karaf.apache.org/manual/latest-2.3.x/developers-guide/creating-bundles.html
http://felix.apache.org/site/apache-felix-maven-bundle-plugin-bnd.html
http://baptiste-wicht.com/posts/2010/03/bundle-non-osgi-dependencies-maven.html
http://maksim.sorokin.dk/it/2011/08/09/maven-apache-felix-strategy-to-handle-non-osgi-dependencies/
- Embed these jar files into your OSGi bundle
- Wrap these jar files within one OSGi bundle
For instance, you need to utilize fastjson library to serialize/deserialize json object in your OSGi bundle.
Embed these jar files into your OSGi bundle
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.4.0</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
<Bundle-Version>${project.version}</Bundle-Version>
<Bundle-Activator>com.example.demo.Activator</Bundle-Activator>
<Export-Package>
com.example.demo*;version=${project.version}
</Export-Package>
<Import-Package>
!org.springframework.*,
*
</Import-Package>
<Embed-Dependency>fastjson;scope=compile|runtime|system;inline=true</Embed-Dependency>
</instructions>
</configuration>
</plugin>
If you want a dependency inlined instead of embedded add the inline=true.
Wrap these jar files within one OSGi bundle
Karaf supports the wrap:protocol execution, so it allows for directly deploying third party dependencies into OSGi container:
install -s wrap:mvn:com.alibaba/fastjson/1.1.37
You can also create a wrap bundle for a third party dependency. This bundle is simply a Maven POM that shades an existing jar and package into a jar bundle.
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.4.0</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
<Bundle-Version>${project.version}</Bundle-Version>
<Import-Package>!org.springframework.*,*</Import-Package>
<Private-Package>!*</Private-Package>
<Embed-Dependency>*;scope=compile;type=!pom;inline=true</Embed-Dependency>
<Export-Package>com.alibaba.fastjson*</Export-Package>
</instructions>
</configuration>
</plugin>
References
http://karaf.apache.org/manual/latest-2.3.x/developers-guide/creating-bundles.html
http://felix.apache.org/site/apache-felix-maven-bundle-plugin-bnd.html
http://baptiste-wicht.com/posts/2010/03/bundle-non-osgi-dependencies-maven.html
http://maksim.sorokin.dk/it/2011/08/09/maven-apache-felix-strategy-to-handle-non-osgi-dependencies/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述