mavenosgiapache-karafbndmaven-bundle-plugin

Override Require-Capability in Maven-Bundle-Plugin


My question is similar to this one but I am using the Maven bundle plugin to achieve the same end result.

I am building a bundle that contains a persistence.xml file and I have found that the maven-bundle-plugin automatically generates the following headers in the manifest:

Require-Capability:osgi.service;effective:=active;objectClass=javax.persistence.spi.PersistenceProvider;javax.persistence.provider=org.apache.openjpa.persistence.PersistenceProviderImpl,
Require-Capability: osgi.extender;osgi.extender=aries.jpa, 
Require-Capability: osgi.service;effective:=active;objectClass=javax.sql.DataSource;filter:="(osgi.jndi.service.name=jdbc/test)"

This in itself is not a problem however I am using Karaf and I want to deploy this and other bundles and Karaf features in one single feature of my own. When I do this it fails because the OSGi is unable to fulfil the capability osgi.service;effective:=active;objectClass=javax.persistence.spi.PersistenceProvider;javax.persistence.provider=org.apache.openjpa.persistence.PersistenceProviderImpl even though I specify the openjpa feature to be installed at the same time. I have discovered that I can get around this issue by changing effective:=active to resolution:=optional

To build my bundle I've tried the following Maven plugin configuration:

<plugin>
    <groupId>org.apache.felix</groupId>
    <artifactId>maven-bundle-plugin</artifactId>
    <version>3.3.0</version>
    <extensions>true</extensions>
    <configuration>
        <instructions>
            <Export-Package>com.example
            </Export-Package>
            <Include-Resource>
                        META-INF/persistence.xml=${project.build.directory}/classes/META-INF/persistence.xml,
                        {maven-resources}
            </Include-Resource>
            <Meta-Persistence>META-INF/persistence.xml</Meta-Persistence>
            <Require-Capability>
                        osgi.service;resolution:=optional;objectClass=javax.persistence.spi.PersistenceProvider;javax.persistence.provider=org.apache.openjpa.persistence.PersistenceProviderImpl,
                        osgi.extender;resolution:=optional;osgi.extender=aries.jpa,
                        osgi.service;resolution:=optional;objectClass=javax.sql.DataSource;filter:="(osgi.jndi.service.name=jdbc/test)"
            </Require-Capability>
        </instructions>
    </configuration>
</plugin>

However I get the same issue as in the linked question above i.e. duplicated requirements in the manifest.

I also see from a link on the above question that a change was made to bnd (bnd issue #1364) but this appears to only work for annotations? Is there a way to configure the Maven plugin to prevent duplicated requirements?

Update #1

My example code is available here at GitHub (karaf_features branch): https://github.com/jtkb/jpatest/tree/feature/karaf_features

It consists of 3 modules but only 2 are of interest for this issue, simple and simple-datasource

simple is the 'persistence unit' and contains the persistence.xml. It is also the bundle in which the 'awkward' (yet real requirements) <Require-Capability> headers are generated.

simple-datasource provides the datasource to the persistence unit and contains a Karaf feature to install simple, simple-datasource bundles and all the required 3rd party bundles (via Karaf features). The feature XML contains:

<feature name="simple-datasource" description="simple-datasource" version="1.0.0.SNAPSHOT">
    <feature version="4.1.1">jdbc</feature>
    <feature version="2.6.0">jpa</feature>
    <feature version="2.4.1">openjpa</feature>
    <feature version="1.0.1">pax-jdbc-mariadb</feature>
    <bundle>mvn:com.javatechnics.jpa/simple-datasource/1.0.0-SNAPSHOT</bundle>
    <bundle>mvn:com.javatechnics.jpa/simple/1.0.0-SNAPSHOT</bundle>
</feature>

So installing my feature in Karaf I get this error:

Error executing command: Unable to resolve root: missing requirement [root] osgi.identity; osgi.identity=simple-datasource; type=karaf.feature; version="[1.0.0.SNAPSHOT,1.0.0.SNAPSHOT]"; filter:="(&(osgi.identity=simple-datasource)(type=karaf.feature)(version>=1.0.0.SNAPSHOT)(version<=1.0.0.SNAPSHOT))"

[caused by: Unable to resolve simple-datasource/1.0.0.SNAPSHOT: missing requirement [simple-datasource/1.0.0.SNAPSHOT] osgi.identity; osgi.identity=com.javatechnics.jpa.simple; type=osgi.bundle; version="[1.0.0.SNAPSHOT,1.0.0.SNAPSHOT]"; resolution:=mandatory

[caused by: Unable to resolve com.javatechnics.jpa.simple/1.0.0.SNAPSHOT: missing requirement [com.javatechnics.jpa.simple/1.0.0.SNAPSHOT] osgi.service; objectClass=javax.persistence.spi.PersistenceProvider; javax.persistence.provider=org.apache.openjpa.persistence.PersistenceProviderImpl; effective:=active]]

The error to me almost feels like a circular reference issue but I cannot see how.

Inspecting the header of the simple bundle:

simple (59)
-----------
Bnd-LastModified = 1513115007378
Build-Jdk = 1.8.0_144
Built-By = kerry
Created-By = Apache Maven Bundle Plugin
Manifest-Version = 1.0
Meta-Persistence = META-INF/persistence.xml
Tool = Bnd-3.2.0.201605172007

Bundle-Blueprint = OSGI-INF/blueprint/blueprint.xml
Bundle-ManifestVersion = 2
Bundle-Name = simple
Bundle-SymbolicName = com.javatechnics.jpa.simple
Bundle-Version = 1.0.0.SNAPSHOT

Export-Service = 
com.javatechnics.jpa.dao.BookServiceDao;ServiceManager=Blueprint;name=
BookServiceDao
Provide-Capability = 
osgi.service;effective:=active;objectClass=javax.persistence.EntityManagerFactory;osgi.unit.name=test,
osgi.service;effective:=active;objectClass=org.apache.aries.jpa.template.JpaTemplate;osgi.unit.name=test,
osgi.service;effective:=active;objectClass=javax.persistence.EntityManager;osgi.unit.name=test,
osgi.service;effective:=active;objectClass=org.apache.aries.jpa.supplier.EmSupplier;osgi.unit.name=test
Require-Capability = 
osgi.service;effective:=active;javax.persistence.provider=org.apache.openjpa.persistence.PersistenceProviderImpl;objectClass=javax.persistence.spi.PersistenceProvider,
osgi.extender;osgi.extender=aries.jpa,
 osgi.service;effective:=active;filter:=(osgi.jndi.service.name=jdbc/test);objectClass=javax.sql.DataSource,
osgi.ee;filter:=(&(osgi.ee=JavaSE)(version=1.5))

Export-Package = 
com.javatechnics.jpa;uses:="com.javatechnics.jpa.dao,javax.persistence";version=1.0.0,
com.javatechnics.jpa.dao;uses:=com.javatechnics.jpa;version=1.0.0
Import-Package = 
com.javatechnics.jpa,
com.javatechnics.jpa.dao,
javax.persistence;version="[1.1,2)",
org.osgi.service.blueprint;version="[1.0.0,2.0.0)"

Solution

  • If you know that a bundle, let’s call it xyz, provides the PersistenceProvider service then you can write one additional bundle that simply does this:

    Require-Bundle: xyz; bundle-version="[...)"
    Provide-Capability: osgi.service;
        objectClass=javax.persistence.spi.PersistenceProvider;
        javax.persistence.provider=org.apache.openjpa.persistence.PersistenceProviderImpl;
        effective:=active
    

    This essentially augments bundle xyz with a capability that will resolve the requirement in your bundle, at the cost of adding an otherwise useless bundle.

    This is still something of a workaround but is better than removing a real requirement from a bundle.