将现有Maven项目迁移到OSGI Bundle中

时间:2012-10-05 09:36:01

标签: java maven osgi rcp

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

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

这是pom.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 clean install很好,我得到了一个包含清单的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 Bundle有什么可能性?

特别是如何管理依赖关系

2 个答案:

答案 0 :(得分:2)

可能一切都在构建中顺利进行。 maven bundle插件会自动为bundle访问的所有包创建import package语句。它甚至可以查看blueprinnt和spring configs。

所以你得到的错误信息

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:protocol来动态创建捆绑包,该协议适用于简单的情况。如果您需要更多,那么您可以创建一个maven项目来将jar包装成一个包。在大多数情况下,这不是必要的。

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

答案 1 :(得分:0)

您的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部分。

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