2

使用 Apache Felix,我编写了一个 OSGi 组件,它封装了我公司使用的一些中间件。目前它依赖于大量的外部库,我似乎遇到了 Bundle-classpath: 参数长度的限制。我不得不将诸如 commons-collections.jar 之类的库重命名为 ccoll.jar。

我很好奇是否有人对解决此限制有任何建议?

Bundle-ClassPath: .,lib/log4j.jar,lib/cvfs.jar,lib/backport.jar,lib/cbeanutils.jar,lib/ccodec.jar,lib/ccoll.jar,lib/chttp.jar,lib/cjxpath.jar,lib/clang.jar,[libs redacted],lib/saaj-api.jar,lib/saaj-impl.jar,lib/Schemas.jar,lib/xbean.jar,lib/clog.jar,lib/dom4j.jar,lib/xml-apis.jar,lib/xerces.jar,lib/xalan.jar,lib/jaxp-ri.jar,lib/japi.jar,lib/mail.jar

我想我可以通过省略 lib/ 位来获得更多字符,但我很好奇这是否是一个错误、一个明确的限制,或者只是我的白痴。

4

5 回答 5

9

清单行长度限制为 72 个字节,如http://java.sun.com/j2se/1.4.2/docs/guide/jar/jar.html中所述。之后,您将不得不拆分该行并开始一个以空格字符开头的新行。在这种情况下:

Bundle-ClassPath: .,lib/log4j.jar,lib/cvfs.jar,lib/backport.jar,lib/cbea
 nutils.jar,lib/ccodec.jar,lib/ccoll.jar,lib/chttp.jar,lib/cjxpath.jar,l
 ib/clang.jar,[libs redacted],lib/saaj-api.jar,lib/saaj-impl.jar,lib/Sch
 emas.jar,lib/xbean.jar,lib/clog.jar,lib/dom4j.jar,lib/xml-apis.jar,lib/
 xerces.jar,lib/xalan.jar,lib/jaxp-ri.jar,lib/japi.jar,lib/mail.jar

或者,您可以使用像 BND 这样的工具来自动为您执行此类(以及更多)操作。

于 2010-07-26T20:56:23.193 回答
4

正如Moritz 所说,每行有72 个字节的限制。

Java jar包包含用于编写清单的代码:

Manifest manifest = new Manifest();
Attributes attributes = manifest.getMainAttributes();
attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0");
attributes
    .putValue(
        "Bundle-Classpath",
        "<snip>");
manifest.write(System.out);

注:Manifest-Version为必填项。

这将产生输出:

Manifest-Version: 1.0
Bundle-Classpath: .,lib/log4j.jar,lib/cvfs.jar,lib/backport.jar,lib/cb
 eanutils.jar,lib/ccodec.jar,lib/ccoll.jar,lib/chttp.jar,lib/cjxpath.j
 ar,lib/clang.jar,[libsredacted],lib/saaj-api.jar,lib/saaj-impl.jar,li
 b/Schemas.jar,lib/xbean.jar,lib/clog.jar,lib/dom4j.jar,lib/xml-apis.j
 ar,lib/xerces.jar,lib/xalan.jar,lib/jaxp-ri.jar,lib/japi.jar,lib/mail
 .jar
于 2010-07-26T21:17:43.070 回答
3

另外,考虑将第三方库打包到他们自己的包中,有些甚至是 osgi-ready。

于 2010-07-26T21:02:23.790 回答
2

查看http://wiki.apache.org/commons/CommonsOsgi了解 Apache-Commons OSGi 就绪库。否则请查看http://www.springsource.com/repository/app/如果他们已经捆绑了您的 3rd 方库。

独立安装这些捆绑包,不要将它们嵌入到您的捆绑包中。

于 2010-07-27T15:59:47.453 回答
1

First, never directly edit MANIFEST.MF. Edit it in a standard text file, for example mymanifest.txt, then pass to the jar command as follows:

jar cfm output.jar mymanifest.txt <other files>

The jar tool will then insert the line-wraps as necessary.

Better answer: use the Bnd tool by Peter Kriens to generate your manifest.

Also as other commenters have pointed out, it is much better to use these libraries as OSGi bundles. Sticking all of your dependencies into one bundle is kind of missing the point of OSGi.

于 2010-08-21T23:10:29.197 回答