无法运行具有Maven依赖关系的OSGi捆绑软件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法运行具有Maven依赖关系的OSGi捆绑软件相关的知识,希望对你有一定的参考价值。

我正在构建OSGi捆绑包(在NetBeans 8.2中,但是我需要外部Maven依赖关系,但是一旦将它们添加到pom中,就不能再运行捆绑包了。

我的整个pom看起来像这样:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>myProject</groupId>
<artifactId>myProject</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>bundle</packaging>

<name>myProject</name>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
    <dependency>
        <groupId>org.osgi</groupId>
        <artifactId>org.osgi.core</artifactId>
        <version>4.3.0</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.15</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-pool2</artifactId>
        <version>2.6.0</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.2</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-dbcp2</artifactId>
        <version>2.5.0</version>
        <scope>compile</scope>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <version>2.3.7</version>
            <extensions>true</extensions>
            <configuration>
                <instructions>
                    <Bundle-Activator>myProject.Activator</Bundle-Activator>
                    <Import-Package>
                        org.osgi.*,
                        javax.*,
                        !org.apache.*,
                        <!-- org.apache.commons.pool2*;version=2.6.0,
                        org.apache.commons.dbcp2*;version=2.5.0,
                        org.apache.commons.logging*;version=1.2,-->
                        !com.mysql.*;version=8.0.15,
                        !com.google.protobuf.*;version=3.6.1
                    </Import-Package>
                    <Export-Package>
                        myProject.service
                    </Export-Package>
                    <Embed-Dependency>
                        commons-dbcp2;scope=compile,
                        commons-logging;scope=compile
                        commons-pool2;scope=compile
                        mysql-connector-java;scope=compile
                    </Embed-Dependency>
                    <Include-Resource>
                        {maven-resources},
                        {maven-dependencies}
                    </Include-Resource>
                </instructions>
            </configuration>
        </plugin>
    </plugins>
</build>

<profiles>
    <profile>
        <id>build-for-felix</id>
        <dependencies>
            <dependency>
                <groupId>org.apache.felix</groupId>
                <artifactId>org.apache.felix.main</artifactId>
                <version>4.0.3</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.apache.felix</groupId>
                <artifactId>org.apache.felix.gogo.shell</artifactId>
                <version>0.10.0</version>
            </dependency>
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <version>1.7</version>
                    <executions>
                        <execution>
                            <id>compile</id>
                            <phase>package</phase>
                            <goals>
                                <goal>run</goal>
                            </goals>
                            <configuration>
                                <target>
                                    <pathconvert property="plugins.jars" pathsep="${path.separator}">
                                        <path refid="maven.runtime.classpath"/>
                                        <map from="${project.build.directory}${file.separator}classes" to=""/>
                                    </pathconvert>
                                    <pathconvert pathsep=" " property="bundles">
                                        <path path="${plugins.jars}"/>
                                        <mapper>
                                            <chainedmapper>
                                                <flattenmapper/>
                                                <globmapper from="*" to="file:modules/*" casesensitive="no"/>
                                            </chainedmapper>
                                        </mapper>
                                    </pathconvert>
                                    <propertyfile file="${project.build.directory}/config.properties">
                                        <entry key="felix.auto.start" value="${bundles} file:modules/${project.build.finalName}.jar"/>
                                        <entry key="org.osgi.framework.bootdelegation" value="*"/>
                                    </propertyfile>
                                    <copy file="${maven.dependency.org.apache.felix.org.apache.felix.main.jar.path}" tofile="${project.build.directory}/felix.jar"/>
                                </target>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <version>2.3</version>
                    <executions>
                        <execution>
                            <id>create-executable-jar</id>
                            <phase>package</phase>
                            <goals>
                                <goal>single</goal>
                            </goals>
                            <configuration>
                                <descriptors>
                                    <descriptor>${basedir}/src/main/assembly/felix.xml</descriptor>
                                </descriptors>
                                <finalName>${project.build.finalName}</finalName>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
    <profile>
        <id>run-on-felix</id>
        <dependencies>
            <dependency>
                <groupId>org.apache.felix</groupId>
                <artifactId>org.apache.felix.main</artifactId>
                <version>4.0.3</version>
                <scope>provided</scope>
            </dependency>
            <!-- org.apache.felix:org.apache.felix.gogo.shell:0.6.1 useless from Maven since stdin is swallowed -->
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <version>1.7</version>
                    <configuration>
                        <target>
                            <property name="vm.args" value=""/>
                            <pathconvert property="plugins.jars" pathsep="${path.separator}">
                                <path refid="maven.runtime.classpath"/>
                                <map from="${project.build.directory}${file.separator}classes" to=""/>
                            </pathconvert>
                            <makeurl property="urls" separator=" ">
                                <path path="${plugins.jars}"/>
                                <path location="${project.build.directory}/${project.build.finalName}.jar"/>
                            </makeurl>
                            <propertyfile file="${project.build.directory}/run.properties">
                                <entry key="felix.auto.start" value="${urls}"/>
                                <entry key="felix.auto.deploy.action" value="uninstall,install,update,start"/>
                                <entry key="org.osgi.framework.storage" value="${project.build.directory}${file.separator}felix-cache"/>
                                <entry key="org.osgi.framework.bootdelegation" value="*"/>
                            </propertyfile>
                            <makeurl property="run.properties.url" file="${project.build.directory}/run.properties"/>
                            <java fork="true" jar="${maven.dependency.org.apache.felix.org.apache.felix.main.jar.path}">
                                <sysproperty key="felix.config.properties" value="${run.properties.url}"/>
                                <jvmarg line="${vm.args}"/>
                            </java>
                        </target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

捆绑软件构建良好,但是当我尝试启动捆绑软件时出现此错误:

[java] org.osgi.framework.BundleException: Unresolved constraint in bundle org.apache.commons.commons-dbcp2 [5]: Unable to resolve 5.0: missing requirement [5.0] osgi.wiring.package; (&(osgi.wiring.package=javax.transaction)(version>=1.1.0))
 [java]     at org.apache.felix.framework.Felix.resolveBundleRevision(Felix.java:3826)
 [java]     at org.apache.felix.framework.Felix.startBundle(Felix.java:1868)
 [java]     at org.apache.felix.framework.Felix.setActiveStartLevel(Felix.java:1191)
 [java]     at org.apache.felix.framework.FrameworkStartLevelImpl.run(FrameworkStartLevelImpl.java:295)
 [java]     at java.lang.Thread.run(Thread.java:748)

编辑:添加了整个pom.xml文件,我已尝试按照类似文章中的建议将所有包添加到其中,但没有帮助。

也试图添加到我的pom中,在这里看不到任何区别。

最后,我尝试添加标签,但这也没有帮助。

pom文件的大部分是由netbeans本身生成的(Maven OSGi Bundle-具有生成的激活器类的默认模板)。

答案

已通过将其添加到pom中来解决:

            <configuration>
                <instructions>
                    <Bundle-Activator>myProject.Activator</Bundle-Activator>
                    <Import-Package>
                       *;resolution:=optional
                    </Import-Package>
                </instructions>
            </configuration>

显然可能存在一些问题,因为您不是直接将软件包导入捆绑包中的类。不确定原因为何,但这是来源:

https://developer.atlassian.com/server/framework/atlassian-sdk/marking-packages-as-optional-imports/

以上是关于无法运行具有Maven依赖关系的OSGi捆绑软件的主要内容,如果未能解决你的问题,请参考以下文章

在 OSGi 包中添加第三方 Maven 依赖项的最佳方法

如何使用 Maven 将第 3 方 OSGi 捆绑包添加到部署包中?

使用不同的启动级别来管理 OSGi 包之间的依赖关系是不是合理?

如何分析 OSGi 部署的执行?

在 Netbeans 和 Karaf 中开发 OSGI 包时如何管理依赖关系?

无法启动 OSGi 包,缺少 com.ibm.uvm.tools