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

时间:2016-05-24 03:41:09

标签: osgi osgi-bundle bnd maven-bundle-plugin

我正在使用maven-bundle-pluginbnd有效)。

直接从源代码中包含资源文件。

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

<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>

让我们有一个依赖:

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

如何引用依赖jar内的资源文件?

换句话说,如何

  • 指定如下内容:

    com.example:library:1.0.0:jar/some.xml
    
  • 而不是:

    target/classes/some.xml
    

以便来自其中一个依赖项的资源出现在输出包jar

2 个答案:

答案 0 :(得分:3)

您可以使用maven-depencency-plugin解压缩您的依赖项jar,然后将资源包含在您的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说明应该是相对的,请参阅Include-Resource,您可以将target替换为${project.build.directory}

答案 1 :(得分:3)

如果您有对jar的文件引用,则可以执行

-includeresource: @path/to/file.jar!/some.xml

您使用@前缀表示资源位于jar中,并使用jar网址中的!/语法。

棘手的部分是从我怀疑的项目依赖项中获取jar的路径。

相关问题