将现有的 Maven 项目迁移到 OSGI bundle中



我是整个OSGi的新手,我的任务是从现有的maven项目中创建一个OSGi捆绑包。

首先,我决定选择最小的部分并从它开始:

这是绒球.xml

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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    <artifactId>cross</artifactId>
    <groupId>net.sf.maltcms</groupId>
    <version>1.2.12-SNAPSHOT</version>
</parent>
<artifactId>cross-main</artifactId>
<packaging>jar</packaging>
<name>cross-main</name>
<dependencies>
    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>cross-annotations</artifactId>
        <version>${project.version}</version>
    </dependency>
    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>cross-event</artifactId>
        <version>${project.version}</version>
    </dependency>
    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>cross-tools</artifactId>
        <version>${project.version}</version>
    </dependency>
    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>cross-exception</artifactId>
        <version>${project.version}</version>
    </dependency>
    <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>1.4</version>
    </dependency>
    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>cross-main-api</artifactId>
        <version>${project.version}</version>
        <exclusions>
            <exclusion>
                <artifactId>commons-logging</artifactId>
                <groupId>commons-logging</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>3.0.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-asm</artifactId>
        <version>3.0.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>3.0.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>3.0.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>3.0.6.RELEASE</version>
        <exclusions>
            <exclusion>
                <artifactId>commons-logging</artifactId>
                <groupId>commons-logging</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-expression</artifactId>
        <version>3.0.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.1</version>
    </dependency>
    <dependency>
        <groupId>net.sf.ehcache</groupId>
        <artifactId>ehcache-core</artifactId>
        <version>2.4.6</version>
    </dependency>
    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>cross-math</artifactId>
        <version>${project.version}</version>
    </dependency>
    <dependency>
        <groupId>com.db4o</groupId>
        <artifactId>db4o-all</artifactId>
        <version>8.0.249</version>
    </dependency>
    <dependency>
        <groupId>net.sf.mpaxs</groupId>
        <artifactId>mpaxs-spi</artifactId>
        <version>1.6.10</version>
    </dependency>
    <dependency>
        <groupId>net.sf.mpaxs</groupId>
        <artifactId>mpaxs-server</artifactId>
        <version>1.6.10</version>
    </dependency>
</dependencies>

我做了一些研究,找到了 maven 的 Apache Bundle 插件,并将 pom 更改为这个

<packaging>bundle</packaging>

并添加

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <extensions>true</extensions>
            <configuration>
                <instructions>          
                    <Bundle-SymbolicName>${pom.artifactId}</Bundle-SymbolicName>
                </instructions>
            </configuration>
        </plugin>
    </plugins>
</build>

MVN全新安装运行良好,我得到了一个包含清单的jar文件,但当然无法解析捆绑包

 BundleException: The bundle "cross-main_1.2.12.SNAPSHOT [30]" could not be resolved. Reason: Missing Constraint: Import-Package: com.db4o; version="[8.0.0,9.0.0)

话短说:将 maven 应用程序迁移到 OSGi 捆绑包的可能性是什么?

在空间上如何管理依赖项

可能一切都在构建中顺利。maven 捆绑包插件会自动为您的捆绑包访问的所有包创建导入包语句。它甚至研究了blueprinnt和spring配置。

所以你得到的错误消息

could not be resolved. Reason: Missing Constraint: 
    Import-Package: com.db4o; version="[8.0.0,9.0.0)"

简单地说,您需要在容器中安装一个包来导出这些包。因此,理想情况下,db4o jar 已经是一个捆绑包。然后你可以简单地安装它。如果没有,那么您将不得不为其创建一个捆绑包。

如果您使用 apache karaf,那么您可以使用 pax url 中的 wrap: 协议即时制作捆绑包,该协议适用于简单情况。如果您需要更多,则可以创建一个maven项目将jar包装成捆绑包。但在大多数情况下,这不是必需的。

我刚刚检查了 db4o 下载。它包含一个 db4o osgi 捆绑包,您可以将其安装在容器中。

您的 maven 文件似乎是正确的。这BundleException你得到:

BundleException: The bundle "cross-main_1.2.12.SNAPSHOT [30]" could not be resolved. Reason: Missing Constraint: Import-Package: com.db4o; version="[8.0.0,9.0.0)

这是因为您必须在捆绑包中导入相应的包。例如,我有捆绑插件:

<plugin>
    <groupId>org.apache.felix</groupId>
    <artifactId>maven-bundle-plugin</artifactId>
    <version>2.3.7</version>
    <extensions>true</extensions>
    <configuration>
        <archive>
            <manifestEntries>
                <Build-Change-Set>${changeSet}</Build-Change-Set>
                <Build-Change-Set-Date>${changeSetDate}</Build-Change-Set-Date>
                <Build-Location>${basedir}</Build-Location>
                <Build-Machine>${env.COMPUTERNAME}</Build-Machine>
                <Build-Date>${maven.build.timestamp}</Build-Date>
            </manifestEntries>
        </archive>
        <instructions>
            <Export-Package>my.bundle.main.package.*,
            </Export-Package>
            <Import-Package>
                org.springframework.context.weaving,
                org.springframework.aop,
                org.springframework.aop.framework,
                org.aopalliance.aop,
                org.apache.cxf.bus.spring,
                com.mycompany.mypackage.that.i.am.using.classess.from,
                *
            </Import-Package>
        </instructions>
    </configuration>
</plugin>

在您的捆绑插件配置中添加此部分:

<instructions>
    <Export-Package>your.bundle.main.package.*,
    </Export-Package>
    <Import-Package>com.db4o,
        *
    </Import-Package>
</instructions>
由于

缺少不同的软件包,您可能会遇到更多这样的错误。因此,请确保将它们全部添加到Import-Package部分。

需要注意的另一件事是:如果您在其他捆绑包中使用捆绑包,则必须导出捆绑包的包,并且在其他捆绑包中,您必须导入捆绑包的包。

最新更新