【问题标题】:Migrate existing Maven Project into an OSGI Bundle将现有的 Maven 项目迁移到 OSGI Bundle
【发布时间】:2012-10-05 09:36:00
【问题描述】:

我是整个 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 Plugin 并将 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 的可能性有哪些?

尤其是如何管理依赖项

【问题讨论】:

    标签: java maven osgi rcp


    【解决方案1】:

    可能在构建中一切顺利。 maven 包插件会自动为您的包访问的所有包创建导入包语句。它甚至可以查看蓝图和弹簧配置。

    所以你得到的错误信息

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

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

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

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

    【讨论】:

    • 如何将此类捆绑包安装到 karaf 容器?
    【解决方案2】:

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

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

    【讨论】:

    • 好的,我添加了以下行 ` com.db4o ` ,重新运行 mvn clean install 并重新安装包,但仍然得到相同的错误...
    • 你加错了,你应该这样加: com.db4o, *
    • 是的,很抱歉我不小心按下了“添加评论”按钮。所以我添加了 com.db4o, * ,重新运行 mvn clean install 生成一个带有以下清单内容(摘录)的 jar: Import-Package: com.db4o;version="[8.0 ,9)",com.db4o.activation;version="[8.0,9)",com.db4o.collections;version="[8.0,9)",com.db4o.config;version="[8.0,9 )",com.db4o.config.annotations;version="[8.0,9)",com.db4o.ext;我重新安装了捆绑包,仍然得到同样的错误。所以看来我是任务的东西,失败了......
    • 最好是再次删除这些语句。上面的错误表明您的 Import-Package 语句已经存在。所以构建很好。它们仅在运行时中丢失。
    猜你喜欢
    • 2015-09-21
    • 1970-01-01
    • 2022-01-07
    • 2020-09-29
    • 2022-07-19
    • 2020-04-08
    • 1970-01-01
    • 1970-01-01
    • 2012-09-26
    相关资源
    最近更新 更多