带有 maven-bundle-plugin 的 OSGI JAR 包

Posted

技术标签:

【中文标题】带有 maven-bundle-plugin 的 OSGI JAR 包【英文标题】:OSGI bundle of JARs with maven-bundle-plugin 【发布时间】:2016-07-20 08:14:43 【问题描述】:

我正在尝试将一些 JAR 嵌入到没有相应 OSGI 包的 OSGI 包中,以便其他 OSGI 包可以引用它们。

我遵循了许多建议,但没有任何效果。我的最后一次尝试是基于:<Export-Package> for all resources using maven-bundle-plugin

当我检查生成的 OSGI 包时,它不包含预期的嵌入式 jar:

META-INF
META-INF\MANIFEST.MF
META-INF\maven
META-INF\maven\com.mycompany
META-INF\maven\com.mycompany\osgi-bundle-test
META-INF\maven\com.mycompany\osgi-bundle-test\pom.properties
META-INF\maven\com.mycompany\osgi-bundle-test\pom.xml

这是我的 POM.XML:

<dependencies>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>18.0</version>
    </dependency>
</dependencies>

<build>
    <defaultGoal>install</defaultGoal>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.8</version>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <version>3.0.1</version>
            <extensions>true</extensions>
            <configuration>
                <manifestLocation>META-INF</manifestLocation>
                <instructions>
                    <Bundle-SymbolicName>$project.artifactId</Bundle-SymbolicName>
                    <Bundle-Version>$project.version</Bundle-Version>
                    <Export-Package>*</Export-Package>
                    <Embed-Dependency>*;scope=compile|runtime</Embed-Dependency>
                    <Embed-Directory>target/dependency</Embed-Directory>
                    <Embed-StripGroup>true</Embed-StripGroup>
                    <Embed-Transitive>true</Embed-Transitive>
                </instructions>
            </configuration>
        </plugin>
    </plugins>
</build>

这是来自 mvn clean install 的 sn-p:

[INFO] --- maven-dependency-plugin:2.8:copy-dependencies (copy-dependencies) @ osgi-bundle-test ---
[INFO] Copying guava-18.0.jar to <local-dir>\target\dependency\guava-18.0.jar
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ osgi-bundle-test ---
[INFO] Building jar: <local-dir>\target\osgi-bundle-test-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ osgi-bundle-test ---
[INFO] Installing <local-dir>\target\osgi-bundle-test-0.0.1-SNAPSHOT.jar to <repo-dir>\com\mycompany\osgi-bundle-test\0.0.1-SNAPSHOT\osgi-bundle-test-0.0.1-SNAPSHOT.jar
[INFO] Installing <local-dir>\pom.xml to <repo-dir>\com\mycompany\osgi-bundle-test\0.0.1-SNAPSHOT\osgi-bundle-test-0.0.1-SNAPSHOT.pom

MANIFEST.MF 的内容:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: xxxx
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_65

请帮忙。

【问题讨论】:

您在清单的 Export-Package 和 Import-Package 中看到了什么? 清单不包含这两个。我已经用清单的内容更新了我原来的问题。 【参考方案1】:

这行得通:

<modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany</groupId>
    <artifactId>osgi-bundle-test-v2</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>bundle</packaging>

    <dependencies>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>18.0</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <version>3.0.1</version>
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <Bundle-SymbolicName>$project.artifactId</Bundle-SymbolicName>
                        <Bundle-Version>$project.version</Bundle-Version>
                        <Export-Package>*</Export-Package>
                        <Embed-Dependency>*;scope=compile|runtime</Embed-Dependency>
                        <Embed-Directory>target/dependency</Embed-Directory>
                    </instructions>
                </configuration>
            </plugin>
        </plugins>
    </build>

生成的包包含 target\dependency\guava-18.0.jar

清单:

Manifest-Version: 1.0
Bnd-LastModified: 1460052892382
Build-Jdk: 1.7.0_79
Built-By: myId
Bundle-ClassPath: .,target/dependency/guava-18.0.jar
Bundle-ManifestVersion: 2
Bundle-Name: osgi-bundle-test-v2
Bundle-SymbolicName: osgi-bundle-test-v2
Bundle-Version: 0.0.1.SNAPSHOT
Created-By: Apache Maven Bundle Plugin
Embed-Dependency: *;scope=compile|runtime
Embed-Directory: target/dependency
Embedded-Artifacts: target/dependency/guava-18.0.jar;g="com.google.guava
 ";a="guava";v="18.0"
Export-Package: com.google.common.annotations;version="18.0.0",com.googl
 e.common.base;version="18.0.0";uses:="javax.annotation",com.google.comm
 on.base.internal;version="0.0.1.SNAPSHOT",com.google.common.cache;versi
 on="18.0.0";uses:="com.google.common.base,com.google.common.collect,com
 .google.common.util.concurrent,javax.annotation",com.google.common.coll
 ect;version="18.0.0";uses:="com.google.common.base,javax.annotation",co
 m.google.common.escape;version="18.0.0";uses:="com.google.common.base,j
 avax.annotation",com.google.common.eventbus;version="18.0.0",com.google
 .common.hash;version="18.0.0";uses:="com.google.common.base,javax.annot
 ation",com.google.common.html;version="18.0.0";uses:="com.google.common
 .escape",com.google.common.io;version="18.0.0";uses:="com.google.common
 .base,com.google.common.collect,com.google.common.hash,javax.annotation
 ",com.google.common.math;version="18.0.0",com.google.common.net;version
 ="18.0.0";uses:="com.google.common.base,com.google.common.collect,com.g
 oogle.common.escape,javax.annotation",com.google.common.primitives;vers
 ion="18.0.0";uses:="com.google.common.base,javax.annotation",com.google
 .common.reflect;version="18.0.0";uses:="com.google.common.collect,javax
 .annotation",com.google.common.util.concurrent;version="18.0.0";uses:="
 com.google.common.base,com.google.common.collect,javax.annotation",com.
 google.common.xml;version="18.0.0";uses:="com.google.common.escape",com
 .google.thirdparty.publicsuffix;version="0.0.1.SNAPSHOT";uses:="com.goo
 gle.common.collect",target.dependency;version="0.0.1.SNAPSHOT"
Import-Package: com.google.common.base;version="[18.0,19)",com.google.co
 mmon.cache;version="[18.0,19)",com.google.common.collect;version="[18.0
 ,19)",com.google.common.escape;version="[18.0,19)",com.google.common.ha
 sh;version="[18.0,19)",com.google.common.io;version="[18.0,19)",com.goo
 gle.common.math;version="[18.0,19)",com.google.common.primitives;versio
 n="[18.0,19)",com.google.common.reflect;version="[18.0,19)",com.google.
 common.util.concurrent;version="[18.0,19)",com.google.thirdparty.public
 suffix;version="[0.0,1)",javax.annotation,sun.misc
Require-Capability: osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=1.6))"
Tool: Bnd-3.0.0.201509101326

【讨论】:

@user2677034 mvn clean install【参考方案2】:

相信你需要在process-classes阶段添加执行部分来执行ma​​nifest goal

<execution>
  <id>bundle-manifest</id>
  <phase>process-classes</phase>
  <goals>
    <goal>manifest</goal>
  </goals>
</execution>

喜欢:

        <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <version>3.0.1</version>
            <extensions>true</extensions>
            <configuration>
                <manifestLocation>META-INF</manifestLocation>
                <instructions>
                    <Bundle-SymbolicName>$project.artifactId</Bundle-SymbolicName>
                    <Bundle-Version>$project.version</Bundle-Version>
                    <Export-Package>*</Export-Package>
                    <Embed-Dependency>*;scope=compile|runtime</Embed-Dependency>
                    <Embed-Directory>target/dependency</Embed-Directory>
                    <Embed-StripGroup>true</Embed-StripGroup>
                    <Embed-Transitive>true</Embed-Transitive>
                </instructions>
            </configuration>
            <executions>
                <execution>
                    <id>bundle-manifest</id>
                    <phase>process-classes</phase>
                    <goals>
                        <goal>manifest</goal>
                    </goals>
                </execution>                        
            </executions>
        </plugin>

【讨论】:

不幸的是,它没有成功。清单文件缺少 Export-Package、Import-Package 和 maven 依赖 jar 文件。

以上是关于带有 maven-bundle-plugin 的 OSGI JAR 包的主要内容,如果未能解决你的问题,请参考以下文章

使用 maven-bundle-plugin 安装 OSGi 依赖项

maven-bundle-plugin 因“无效的类文件 module-info.class”而失败

为啥(以及赞成啥)maven-bundle-plugin 的 wrap/bundleall 目标被弃用?

将 maven-bundle-plugin 与 maven-shade-plugin 一起使用

如何使用 bnd/maven-bundle-plugin 从 jar 依赖项将资源文件包含到 osgi 包中?

<Export-Package> 用于使用 maven-bundle-plugin 的所有资源