在 Maven-Bundle-Plugin 中覆盖 Require-Capability

Override Require-Capability in Maven-Bundle-Plugin

我的问题与 this one 类似,但我使用 Maven 捆绑插件来实现相同的最终结果。

我正在构建一个包含 persistence.xml 文件的包,我发现 maven-bundle-plugin 在清单中自动生成以下 header:

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)"

这本身不是问题,但是我使用的是 Karaf,我想在我自己的一个功能中部署这个和其他包以及 Karaf 功能。当我这样做时它失败了,因为 OSGi 无法实现 osgi.service;effective:=active;objectClass=javax.persistence.spi.PersistenceProvider;javax.persistence.provider=org.apache.openjpa.persistence.PersistenceProviderImpl 功能,即使我指定要同时安装 openjpa 功能。我发现我可以通过将 effective:=active 更改为 resolution:=optional

来解决这个问题

为了构建我的包,我尝试了以下 Maven 插件配置:

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

但是我遇到了与上述 linked 问题相同的问题,即清单中的重复要求。

我还从关于上述问题的 link 中看到对 bnd 进行了更改(bnd 问题 #1364),但这似乎只适用于注释?有没有办法配置 Maven 插件来防止重复的需求?

更新 #1

我的示例代码位于 GitHub(karaf_features 分支): https://github.com/jtkb/jpatest/tree/feature/karaf_features

它由 3 个模块组成,但只有 2 个模块与此问题相关,simplesimple-datasource

simple 是 'persistence unit' 并包含 persistence.xml。它也是生成 'awkward'(实际需求)<Require-Capability> header 的包。

simple-datasource 为持久性单元提供数据源,并包含一个 Karaf 功能来安装 simplesimple-datasource 包和所有必需的第三方包(通过 Karaf 功能)。特征 XML 包含:

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

因此在 Karaf 中安装我的功能时出现此错误:

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]]

这个错误对我来说几乎就像一个循环引用问题,但我看不出是怎么回事。

正在检查 simple 包的 header:

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)"

如果您知道一个包,我们称它为xyz,提供PersistenceProvider服务,那么您可以编写一个额外的包来简单地执行此操作:

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

这实质上增强了捆绑包 xyz 的功能,可以解决您的捆绑包中的需求,但代价是添加了一个无用的捆绑包。

这仍然是一种解决方法,但比从捆绑包中删除实际要求要好。