jar - 使用 maven-bundle-plugin 在 JAR 中嵌入依赖项不起作用

标签 jar osgi osgi-bundle flying-saucer maven-bundle-plugin

我成功地将 OSGi 特定元数据添加到 Flying-saucer-pdf Maven 工件的 MANIFEST 中。但是,我没有成功将该工件的依赖项和传递依赖项嵌入到创建的 JAR 文件中。

我使用了来自 GitHub [1] 的 Flying-saucer-pdf 的原始来源,并将以下语句添加到 pom.xml 文件中:

<plugin>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
      <archive>
        <manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
      </archive>
    </configuration>
  </plugin>
  <plugin>
    <groupId>org.apache.felix</groupId>
    <artifactId>maven-bundle-plugin</artifactId>
    <executions>
      <execution>
        <id>bundle-manifest</id>
        <phase>process-classes</phase>
        <goals>
          <goal>manifest</goal>
        </goals>
        <configuration>
          <instructions>
            <Embed-Dependency>itext</Embed-Dependency>
            <Embed-Transitive>true</Embed-Transitive>
          </instructions>
        </configuration>
      </execution>
    </executions>
  </plugin>

该工件在其原始 pom.xml 中声明了对 itext 版本 2.1.7 的依赖项,我没有触及该依赖项。我也没有弄乱工件的原始包装类型,即 jar。

不幸的是,这仅完成了部分工作。 MANIFEST.MF 似乎正确并且包含预期的 OSGi 标签:

Manifest-Version: 1.0
Bundle-Description: Flying Saucer is a CSS 2.1 renderer written in Jav
 a.  This artifact supports PDF output.
Bundle-License: http://www.gnu.org/licenses/lgpl.html
Bundle-SymbolicName: org.xhtmlrenderer.flying-saucer-pdf
Archiver-Version: Plexus Archiver
Built-By: u0400072
Bnd-LastModified: 1478168053263
Bundle-ManifestVersion: 2
Embed-Dependency: itext
Import-Package: com.apple.mrj,com.lowagie.toolbox,javax.crypto,javax.i
 mageio,javax.imageio.metadata,javax.imageio.plugins.jpeg,javax.imagei
 o.stream,javax.swing,javax.xml.parsers,javax.xml.transform,javax.xml.
 transform.dom,javax.xml.transform.sax,javax.xml.transform.stream,org.
 bouncycastle.asn1,org.bouncycastle.asn1.cmp,org.bouncycastle.asn1.cms
 ,org.bouncycastle.asn1.ocsp,org.bouncycastle.asn1.pkcs,org.bouncycast
 le.asn1.tsp,org.bouncycastle.asn1.x509,org.bouncycastle.cms,org.bounc
 ycastle.crypto,org.bouncycastle.crypto.engines,org.bouncycastle.crypt
 o.modes,org.bouncycastle.crypto.paddings,org.bouncycastle.crypto.para
 ms,org.bouncycastle.jce.provider,org.bouncycastle.ocsp,org.bouncycast
 le.tsp,org.w3c.dom,org.xhtmlrenderer.context,org.xhtmlrenderer.css.co
 nstants,org.xhtmlrenderer.css.extend,org.xhtmlrenderer.css.parser,org
 .xhtmlrenderer.css.sheet,org.xhtmlrenderer.css.style,org.xhtmlrendere
 r.css.style.derived,org.xhtmlrenderer.css.value,org.xhtmlrenderer.ext
 end,org.xhtmlrenderer.layout,org.xhtmlrenderer.render,org.xhtmlrender
 er.resource,org.xhtmlrenderer.simple.extend,org.xhtmlrenderer.swing,o
 rg.xhtmlrenderer.util,org.xml.sax,org.xml.sax.helpers
Require-Capability: osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=1.6))"
Tool: Bnd-3.2.0.201605172007
Embedded-Artifacts: itext-2.1.7.jar;g="com.lowagie";a="itext";v="2.1.7
 "
Export-Package: org.xhtmlrenderer.pdf;uses:="org.w3c.dom,org.xhtmlrend
 erer.css.constants,org.xhtmlrenderer.css.parser,org.xhtmlrenderer.css
 .style,org.xhtmlrenderer.css.value,org.xhtmlrenderer.extend,org.xhtml
 renderer.layout,org.xhtmlrenderer.render,org.xhtmlrenderer.resource,o
 rg.xhtmlrenderer.simple.extend,org.xhtmlrenderer.swing,org.xml.sax";v
 ersion="9.0.10",org.xhtmlrenderer.pdf.util;uses:="org.w3c.dom,org.xht
 mlrenderer.pdf";version="9.0.10",org.xhtmlrenderer.simple;uses:="java
 x.swing,org.w3c.dom,org.xhtmlrenderer.css.extend,org.xhtmlrenderer.cs
 s.sheet,org.xhtmlrenderer.extend,org.xhtmlrenderer.layout,org.xhtmlre
 nderer.swing,org.xhtmlrenderer.util";version="9.0.10"
Bundle-Name: Flying Saucer PDF Rendering
Bundle-Version: 9.0.10.SNAPSHOT
Bundle-ClassPath: .,itext-2.1.7.jar
Embed-Transitive: true
Created-By: Apache Maven Bundle Plugin
Build-Jdk: 1.8.0_102

但是 itext 库没有放入生成的 JAR 中,即 MANIFEST 的 Bundle-ClassPath 条目指向缺失的内容。

此外,我尝试创建一个全新的工件,它声明了对原始 Flying-saucer-pdf 工件的依赖关系,并将其重新捆绑为 OSGi 包,在 StackOverflow 上传递此答案 [2],并且这有效。

我能看到的唯一真正的区别是包装类型“bundle”与“jar”。但我无法更改原始 Flying-saucer-pdf 工件中的打包类型,因为非 OSGi 使用的所有内容都需要保持原样,以便使该更改作为推送请求被接受。

你们知道这种与 maven-bundle-plugin 的嵌入依赖关系是否真的可以与打包类型“jar”一起使用吗?或者它是否需要包装类型“捆绑”?

我很感激任何回应和暗示我可以尝试如何直接在原始工件中完成重新捆绑。

非常感谢。

问候 蒂莫·罗尔伯格

最佳答案

有两种方法可以使用maven包插件。

第一种方式是使用

<extensions>true</extensions> and <packaging>bundle</packaging>

在这种情况下,maven 包插件负责所有构建步骤,并且可以影响 jar 文件内容。

第二种方法是使用 list 目标并将 list 添加到 jar 插件中。在这种情况下,maven bundle 插件只能影响 jar。它不能嵌入任何其他库或复制外部私有(private)类。

因此,如果您需要嵌入,那么唯一的方法就是更改包装。

所以我认为有两种解决方案不会对原始jar产生太大影响。

  1. 不要将依赖项作为 bundle 嵌入和安装
  2. 在构建中创建一个 spearate 模块以创建一个 bundle ,然后该 bundle 可附加到原始 jar

关于jar - 使用 maven-bundle-plugin 在 JAR 中嵌入依赖项不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40399144/

相关文章:

java - 使用字节数组而不是文件位置安装 osgi 包?

java - 处理 JAR 和开发中的资源文件路径

java - 带有 IntelliJ 的 opencsv

java - 如何从头开始为 bundle 使用 org.apache.felix.scr 注释?

java - 有没有办法从另一个正在运行的包中激活/运行 osgi Bundle?

java - 如何让MongoDB服务可用?

java - 将 JAR 转换为 Swing 组件

java - 如何将项目转换为maven?

java - OSGi Bundle 状态不是 Active(CQ5 Maven 项目)为什么?

java - 捆绑 OSGi 依赖库的标准方法是什么?