Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include resource file into osgi bundle from jar dependency with bnd/maven-bundle-plugin?

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

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

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>

How to reference resource file inside dependent jar?

In other words, how to

  • specify something like this:

    com.example:library:1.0.0:jar/some.xml
    
  • instead of this:

    target/classes/some.xml
    

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

like image 869
uvsmtid Avatar asked May 24 '16 03:05

uvsmtid


People also ask

What does Maven Bundle Plugin do?

The maven-bundle-plugin provides a simple goal to check for missing bundles, and remove them from the local OBR. Configuration: obrRepository path to local OBR, defaults to <local-maven-repository> /repository. xml.

Which Maven plugin is required for OSGi declarative services?

Using the Apache Felix Maven SCR Plugin to generate Declarative Services and Metatype Service descriptors during a Maven Build.

What is Maven bundled?

This Maven plugin is based on the BND tool from Peter Kriens. The way BND works is by treating your project as a big collection of classes (e.g., project code, dependencies, and the class path).


2 Answers

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

like image 85
Alexandre Cartapanis Avatar answered Oct 31 '22 16:10

Alexandre Cartapanis


If you have a file reference to the jar, you can do

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

You use the @ prefix to say the resource is in the jar and the !/ syntax from jar urls.

The tricky part will be getting a path to the jar from the project dependencies I suspect.

like image 4
BJ Hargrave Avatar answered Oct 31 '22 15:10

BJ Hargrave