【问题标题】:Embedding Dependency in JAR with maven-bundle-plugin not working使用 maven-bundle-plugin 在 JAR 中嵌入依赖项不起作用
【发布时间】:2017-03-16 21:34:02
【问题描述】:

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

我使用的是从 GitHub [1] 获取的来自 fly-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 条目指向缺少的内容。

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

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

你们知道这个嵌入依赖与 maven-bundle-plugin 是否真的可以使用包装类型“jar”吗?还是需要打包类型'bundle'?

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

非常感谢。

问候 蒂莫·罗尔伯格

【问题讨论】:

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


    【解决方案1】:

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

    第一种方式是使用

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

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

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

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

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

    1. 不要将依赖项作为包嵌入和安装
    2. 在构建中创建一个单独的模块以创建一个捆绑包,然后可以在原始 jar 之外使用它

    【讨论】:

      猜你喜欢
      • 2012-04-20
      • 2020-03-02
      • 2010-11-25
      • 1970-01-01
      • 2018-09-20
      • 1970-01-01
      • 2017-06-15
      • 1970-01-01
      • 2014-09-09
      相关资源
      最近更新 更多