覆盖Maven-Bundle-Plugin

时间:2017-12-10 11:33:17

标签: maven osgi apache-karaf bnd maven-bundle-plugin

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

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

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>

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

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

更新#1

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

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

simple是'持久单元'并包含persistence.xml。它也是生成“笨拙”(但真正的要求)<Require-Capability>标题的包。

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中安装我的功能我收到此错误:

  

执行命令时出错:无法解析root:缺少需求[root] osgi.identity; osgi.identity =简单的数据源;键入= karaf.feature;版本= “[1.0.0.SNAPSHOT,1.0.0.SNAPSHOT]”; filter:=“(&amp;(osgi.identity = simple-datasource)(type = karaf.feature)(version&gt; = 1.0.0.SNAPSHOT)(version&lt; = 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;版本= “[1.0.0.SNAPSHOT,1.0.0.SNAPSHOT]”;分辨率:=强制性

     

[由:无法解决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;有效:=活性]]

对我来说这个错误几乎就像一个循环引用问题,但我看不出如何。

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

1 个答案:

答案 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的功能,该功能将解决捆绑中的需求,但代价是添加一个无用的捆绑包。

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