In my maven OSGi project, I use the maven-bundle-plugin
to create my bundle jar.
The git-commit-id-maven-plugin
generates to target/git.properties by default, and they recommend to generate to /target/classes/git.properties to ensure that the file gets added to the /classes directory inside the jar:
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>revision</goal>
</goals>
</execution>
</executions>
<configuration>
<dotGitDirectory>${project.basedir}/../.git</dotGitDirectory>
<generateGitPropertiesFile>true</generateGitPropertiesFile>
<generateGitPropertiesFilename>
${project.build.outputDirectory}/classes/git.properties
</generateGitPropertiesFilename>
<includeOnlyProperties>
<includeOnlyProperty>^git.build.(time|version)$</includeOnlyProperty>
<includeOnlyProperty>^git.commit.id.(abbrev|full)$</includeOnlyProperty>
</includeOnlyProperties>
<commitIdGenerationMode>full</commitIdGenerationMode>
</configuration>
</plugin>
That works, the file is present in the resulting jar, but I am unable to read the file from within my OSGi bundle.
The reason why the classloader cannot find the resource file at runtime, although it is contained in the bundle, is that the resource file is not declared in META-INF/MANIFEST.MF.
Normally, the
maven-bundle-plugin
automatically adds everything inside src/main/resources to the manifest. This is why the plugin ignores target/classes/git.properties - themaven-bundle-plugin
ignores that location.You can add additional resources to the manifest using an
<Include-Resource/>
instruction in the configuration of themaven-bundle-plugin
. Note, however:-- Felix Bundle Plugin Documentation
In my case I wanted my resource to be detected by the
maven-bundle-plugin
automatically, so I decided to have thegit-commit-id-plugin
generate its output to target/generated-resources/git.properties:Next, I added the target/generated-resources folder to the Maven reactor using the
build-helper-maven-plugin
:In any case, what you need to achieve is that the resource is present in META_INF/MANIFEST.MF, like so:
Only then code like the following will work:
As a sidenote, in case you want to use the excellent
git-commit-id-maven-plugin
: the example above is for V.4.0.0 of that plugin, which works up to Java 8. There are more recent versions, but those have different Maven coordinates. For details, refer to the plugin's documentation.