如何使用' bnd-maven-plugin'在我的OSGi捆绑jar中添加所有第三方jar(可解析)?

时间:2016-10-20 14:25:28

标签: osgi osgi-bundle bnd embedded-osgi

我正在使用' bnd-maven-plugin'开发OSGi应用程序。 https://github.com/bndtools/bnd/tree/master/maven/bnd-maven-plugin

我的项目中有很多第三方罐子,通过maven引用。

当我使用' maven install'创建jar包时,我会得到它,当我在felix上部署它时,它将无法解析其他依赖的第三方jar。

正在使用' maven-bundle-plugin' http://www.lucamasini.net/Home/osgi-with-felix/creating-osgi-bundles-of-your-maven-dependencies

POM文件包含' bnd-maven-plugin '如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>XYZ.Models</groupId>
    <artifactId>XYZ.Models</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <dependencies>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.4</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.2.4</version>
        </dependency>

    </dependencies>

    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>biz.aQute.bnd</groupId>
                <artifactId>bnd-maven-plugin</artifactId>
                <version>3.2.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>bnd-process</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.0.0</version>
                <configuration>
                    <archive>

                        <manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

下面是一个正在使用的POM,用于&#39; maven-bundle-plugin &#39;

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>XYZ.Models</groupId>
    <artifactId>XYZ.Models</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <dependencies>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.4</version>
            <scope>provided</scope>
        </dependency>


        <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.2.4</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <id>create-osgi-bundles-from-dependencies</id>
            <build>
                <directory>${basedir}/bundles</directory>
                <plugins>

                    <plugin>
                        <groupId>org.apache.felix</groupId>
                        <artifactId>maven-bundle-plugin</artifactId>
                        <version>2.0.1</version>
                        <extensions>true</extensions>
                        <executions>
                            <execution>
                                <id>wrap-my-dependency</id>
                                <goals>
                                    <goal>wrap</goal>
                                </goals>
                                <configuration>
                                    <wrapImportPackage>;</wrapImportPackage>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

2 个答案:

答案 0 :(得分:1)

maven-bundle-plugin中的魔力由<wrapImportPackage>;</wrapImportPackage>完成,它基本上会将所有依赖项作为资源添加到生成的jar文件中,并为您配置bundle类路径。

bnd-maven-plugin不使用POM中的说明。您必须在bnd文件中执行此操作。 This tutorial特定于Liferay和Gradle,但它会向您显示-includeresource指令的示例以及如何设置捆绑类路径。

答案 1 :(得分:0)

如上所述,正确地提到了bnd-maven-plugin。您也可以从bndtools和-wcm.io中获取参考,但是如果有帮助,只需添加几行。

 <configuration>
     <bnd><![CDATA[
         Include-Resource: <EXAMPLE-GROUP-ID>*.jar;<ANOTHER>.*;
     ]]></bnd>
</configuration>

您可以根据需要使用通配符。我刚刚添加了示例。