如何在 OSGi 中使用 Apache POI [英] How to use Apache POI in OSGi

查看:50
本文介绍了如何在 OSGi 中使用 Apache POI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 OSGi 中使用 Apache POI 来编写一个带有流式处理的 Excel 工作簿,OOXML API (SXSSF).流媒体 API 从 POI 3.9 开始可用.

I want to use Apache POI in OSGi to write an Excel workbook with the streaming, OOXML API (SXSSF). The streaming API is available since POI 3.9.

由于最新的 Apache POI 3.11 jar 不是捆绑包:让 POI 在 OSGi 中工作的最佳方法是什么?

As the latest Apache POI 3.11 jars are not bundles: What's the best way to get POI working in OSGi?

我尝试了两种方法:

  1. 将 jars 直接嵌入到将使用它们的唯一包中
  2. 使用预包装为捆绑包的 POI 罐

我对将所有依赖项整合在一起感到绝望.

I am despairing in getting all dependencies together.

首先关于在我的包中嵌入 POI jar:我的 bndtools 文件包含

First about embedding the POI jar in my bundle: my bndtools file contains

-buildpath:  \
...
libs/dom4j-1.6.1.jar;version=file,\
libs/poi-3.11.jar;version=file,\
libs/poi-ooxml-3.11.jar;version=file,\
libs/poi-ooxml-schemas-3.11.jar;version=file

Private-Package:  \
...
org.openxmlformats.schemas.*,\
org.apache.poi.*,\
org.dom4j.*,\
com.microsoft.schemas.office.x2006.*,\
schemaorg_apache_xmlbeans.*,\
schemasMicrosoftComOfficeExcel.*,\
schemasMicrosoftComOfficeOffice.*,\
schemasMicrosoftComVml.*

这会生成一个包,其中导入了很多很多东西,例如 org.bouncycastle.asn1.x509org.junit.我不打算在我的应用程序中加密或测试 - 所以这两个可能在某种程度上是可选的".我该如何指定?有没有收集所有这些依赖项的好方法?

This results in a bundle which imports many, many things like for example org.bouncycastle.asn1.x509 and org.junit. I don't plan to encrypt or test in my application - so these two are probably somehow "optional". How can I specify this? Is there a good way of collecting all these dependencies?

注意:至少还需要 org.apache.commons.codeccom.sun.msv.datatype.xsd.lib,但它们已经是捆绑包.

Note: at least org.apache.commons.codec and com.sun.msv.datatype.xsd.lib are additionally required, but they are already bundles.

使用预包装的 jars,我尝试使用 org.apache.servicemix.bundles.poi 3.9_2.这也需要 dom4j 所以我使用了预包装的 org.apache.servicemix.bundles.dom4j 但这至少需要 javax.xml.stream0.0.0.1_007_JavaSE.我手动修复了这个问题(丑陋),但后来卡在了另一个依赖项上.

Using prewrapped jars, I tried using org.apache.servicemix.bundles.poi 3.9_2. This also requires dom4j so I used the prewrapped org.apache.servicemix.bundles.dom4j but that requires at least version 1.0 of javax.xml.stream which my JVM/Felix OSGi advertises as "only" version 0.0.0.1_007_JavaSE. I fixed this by hand (ugly), but then got stuck on another dependency.

什么是好方法?

推荐答案

我们使用 Gradle 和 bnd-platform 基于 Maven 依赖关系为我们的应用程序构建 OSGi 包.不确定这是否是好方法",但这就是我们为基于 OSGi 的应用程序构建目标平台的方式,Apache POI 就是其中的一部分.在您必须对包进行调整(例如,将 JUnit 设为可选)或合并 JAR(例如,由于 OSGi 中的类加载问题)以使其工作的情况下,它特别有用.

We use Gradle with bnd-platform to build OSGi bundles for our applications based on Maven dependencies. No sure if this is "the good way", but this is how we build the target platform for our OSGi based applications, Apache POI being part of that. It's especially useful in cases where you have to do adaptions to bundles (e.g. make JUnit optional) or merge JARs (e.g. due to classloading issues in OSGi) to make them work.

我设置了一个 示例构建使用 GitHub 上的 Apache POI 包(隐含地,其 POM 定义的依赖项).您可以克隆它(sample-poi 分支)并尝试它运行 ./gradlew clean bundles.创建的包将在 build/plugins 中.

I set up an example build with an Apache POI bundle (and implicitly, its POM-defined dependencies) on GitHub. You can clone it (sample-poi branch) and try it running ./gradlew clean bundles. Created bundles will be in build/plugins.

请注意,默认情况下不会包含任何可选的 Maven 依赖项,如果需要,必须手动添加到构建中(由于 Gradle 中的限制).

Please note that any optional Maven dependencies will not be included by default and have to be added manually to the build, if you need them (due to limitations in Gradle).

这篇关于如何在 OSGi 中使用 Apache POI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆