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

问题描述:

我正在使用maven-bundle-plugin(实际上是bnd).

I'm using maven-bundle-plugin (bnd effectively).

从源代码中包含资源文件很简单.

It's straightforward to include a resource file from sources.

例如,在构建期间,资源文件(src/main/resources/some.xml)被移动到target目录(target/classes/some.xml)下,并且可以使用<Include-Resource>指令将其包含在捆绑软件中:

For example, a resource file (src/main/resources/some.xml) is moved under target directory (target/classes/some.xml) during build time and can be included into the bundle using <Include-Resource> instruction:

<plugin>
    <groupId>org.apache.felix</groupId>
    <artifactId>maven-bundle-plugin</artifactId>
    <version>3.0.1</version>
    <extensions>true</extensions>
    <configuration>
        <instructions>
            <Include-Resource>
                some.xml=target/classes/some.xml,
            </Include-Resource>
        </instructions>
    </configuration>
</plugin>

让我们有一个依存关系:

Let us have a dependency:

<dependency>
    <groupId>com.example</groupId>
    <artifactId>library</artifactId>
    <version>1.0.0</version>
</dependency>

如何在从属jar内部引用资源文件?

How to reference resource file inside dependent jar?

换句话说,怎么做

  • 指定类似这样的内容:

  • specify something like this:

com.example:library:1.0.0:jar/some.xml

  • 代替此:

  • instead of this:

    target/classes/some.xml
    

  • 是来自依赖项之一的资源出现在输出束jar中吗?

    so that resource from one of the dependency appeared in output bundle jar?

    您可以使用maven-dependency-plugin解压缩依赖项jar,然后将资源包括在jar中.

    You can use the maven-dependency-plugin to un-compress your dependencies jar and then include the resource in your jar.

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <executions>
            <execution>
                <id>unpack-dependencies</id>
                <phase>generate-resources</phase>
                <goals>
                    <goal>unpack</goal>
                </goals>
                <configuration>
                    <markersDirectory>${project.build.directory}/dependencies/dependency-maven-plugin-markers</markersDirectory>
                    <artifactItems>
                        <artifactItem>
                            <groupId>DEPENDENCY_GROUPID</groupId>
                            <artifactId>DEPENDENCY_ARTIFACTID</artifactId>
                            <type>OPTIONAL_DEPENCENCY_TYPE</type>
                            <outputDirectory>${project.build.directory}/dependencies/DEPENDENCY_ARTIFACTID</outputDirectory>
                        </artifactItem>
                    </artifactItems>
                </configuration>
            </execution>
        </executions>
    </plugin>
    ...
    <plugin>
        <groupId>org.apache.felix</groupId>
        <artifactId>maven-bundle-plugin</artifactId>
        <configuration>
            ...
            <instructions>
                ...
                <Include-Resource>target/dependencies/DEPENDENCY_ARTIFACTID/some.xml</Bundle-Activator>
            </instructions>
        </configuration>
    </plugin>
    

    Include-Resource指令应该是pom相对的,请参见

    The Include-Resource instructions is supposed to be pom relative, see Include-Resource, you can probably replace targetwith ${project.build.directory}.