8

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?

uvsmtid
  • 4,187
  • 4
  • 38
  • 64
  • I don't understand the question. Are you asking how to reference some.xml at runtime from code inside the bundle? – BJ Hargrave May 24 '16 at 12:55
  • I just want to package bundle `jar` with `some.xml` taken from inside of another (dependency) `jar`. Ultimately, this will be referenced by code, but before it can be referenced by code, it should be packaged into output `jar`. – uvsmtid May 24 '16 at 13:09

2 Answers2

8

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

lealceldeiro
  • 14,342
  • 6
  • 49
  • 80
Alexandre Cartapanis
  • 1,513
  • 3
  • 15
  • 19
  • Honestly, I expected there could be a trick from `maven-bundle-plugin`/`bnd` to select resources to be moved into output bundle `jar` similarly as the plugin does when `Private-Package` instruction specified with packages (and these packages become part of the output bundle `jar`). However, this answer is straightforward and goes around by cutting off some responsibilities from the sophistication of `maven-bundle-plugin`. – uvsmtid May 24 '16 at 14:44
4

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.

BJ Hargrave
  • 9,324
  • 1
  • 19
  • 27
  • Indeed. I don't know how to reliably reference copy of the `jar` file which is listed as Maven dependency. So, the answer with `maven-dependency-plugin` was acceptable and fixed the problem for now. However, I might need your piece of information too - upvoting. Thanks! – uvsmtid May 26 '16 at 02:32
  • For more info, you can get a path to a jar using the (I think undocumented) `repo` macro, e.g: `-includeresource: @${repo;org.apache.felix.http;latest}!/META-INF/LICENSE` You can even do things like this (but not in the cnf project as I guess the repos haven't been defined yet): `-include: jar:${fileuri;${repo;bsn;version}}!/resource.properties` – Elias Vasylenko Jan 05 '17 at 11:03
  • This question is about a maven build, so their is no bnd workspace and so it is unlikely any repos are configured for the repo macro to be of any use. – BJ Hargrave Jan 05 '17 at 16:03
  • Oh dear, right you are! I just came from Google with my slightly related problem without properly understanding the context. Well, hopefully it will help someone out all the same. – Elias Vasylenko Mar 23 '17 at 22:26