8

我正在使用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

4

2 回答 2

7

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>

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

于 2016-05-24T14:04:44.987 回答
4

如果你有对 jar 的文件引用,你可以这样做

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

您使用@前缀表示资源在 jar 中以及!/来自 jar url 的语法。

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

于 2016-05-24T18:24:01.470 回答