覆盖 Maven-Bundle-Plugin 中的 Require-Capability [英] Override Require-Capability in Maven-Bundle-Plugin

查看:31
本文介绍了覆盖 Maven-Bundle-Plugin 中的 Require-Capability的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题类似于这个 但我使用 Maven 捆绑插件来实现相同的最终结果.

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

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

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

这本身不是问题,但是我使用的是 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

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

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

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.

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

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?

我的示例代码可在 GitHub(karaf_features 分支)上找到:https://github.com/jtkb/jpatest/tree/feature/karaf_features

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

它由 3 个模块组成,但只有 2 个对这个问题感兴趣,simplesimple-datasource

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

simple 是持久化单元",包含 persistence.xml.它也是生成尴尬"(但实际需求) 标头的包.

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 为持久化单元提供数据源,并包含一个 Karaf 功能来安装 simplesimple-datasource 包和所有所需的第 3 方捆绑包(通过 Karaf 功能).功能 XML 包含:

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>

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

So installing my feature in Karaf I get this error:

错误执行命令:无法解析root:缺少需求[root] osgi.identity;osgi.identity=简单数据源;类型=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))"

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

[由:无法解析 simple-datasource/1.0.0.SNAPSHOT:缺少需求 [simple-datasource/1.0.0.SNAPSHOT] osgi.identity;osgi.identity=com.javatechnics.jpa.simple;类型=osgi.bundle;version="[1.0.0.SNAPSHOT,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

[由:无法解析 com.javatechnics.jpa.simple/1.0.0.SNAPSHOT:缺少需求 [com.javatechnics.jpa.simple/1.0.0.SNAPSHOT] osgi.service;objectClass=javax.persistence.spi.PersistenceProvider;javax.persistence.provider=org.apache.openjpa.persistence.PersistenceProviderImpl;有效:=活动]]

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

检查 simple 包的头部:

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

推荐答案

如果你知道一个bundle,我们称之为xyz,它提供了PersistenceProvider 服务然后你可以编写一个额外的包来简单地做到这一点:

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

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

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.

这篇关于覆盖 Maven-Bundle-Plugin 中的 Require-Capability的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆