将现有java项目转换为osgi包的最佳方法

Ara*_*ram 9 java eclipse osgi maven

我们有许多组件,我们想要开始模块化只有少数组件.想知道在所有这些组件中创建捆绑包的最佳方式(在我的构建环境中)是什么?

我的环境:Java 6,Maven 2.2.1,Hudson

技术:Spring 3.0.5,WebSphere 7,Hibernate 3.2.x和大多数apache commons.

要求

  1. 模块化只有少数组件.其余组件可以导出所有包.
  2. 当导入到eclipse中时,我应该能够在构建路径中看到导入包的捆绑包作为依赖项(mvn eclipse:eclipse似乎不会这样做)

ear*_*cam 11

首先只更改MANIFEST.MF条目,使所有工件成为捆绑 - 它们显然不会神奇地工作,但它是一个很好的非破坏性的第一步.

当使用maven-bundle-plugin确保你设置extensions并且supportedProjectTypes你可能遇到CI构建问题时,如果打包类型是Maven repos和m2e失败bundle(参见结束).

尽早测试最危险/最核心的外部依赖项 - 例如,如果您使用JPA进行持久化,那么请确保提供程序在OSGi环境中使用您的域绑定和JDBC驱动程序.

如果您从Java EE/spring迁移到Karaf或Virgo.但是,如果您的组件是用于嵌入式系统或没有外部依赖,那么Felix或Equinox可能就足够了(尽管如果是这样,请查看pax-url项目).

可能值得编辑您的问题,以更具体地了解域/技术?


eclipse:eclipse只会在项目首次配置时生成,m2e的生命周期问题可能会有点痛苦,但它比使用旧的eclipse插件要好得多.


以下内容将向现有工件添加清单条目,而不以任何其他方式更改它们.它告诉标准的maven jar和war插件使用maven-bundle-plugin生成的MANIFEST.MF.

把它放在父POM中:

<pluginManagement>
<plugin>
    <groupId>org.apache.felix</groupId>
    <artifactId>maven-bundle-plugin</artifactId>
    <version>2.3.7</version>
    <extensions>true</extensions>
    <configuration>
        <archive>
            <addMavenDescriptor>true</addMavenDescriptor>
        </archive>
        <supportedProjectTypes>
            <supportedProjectType>jar</supportedProjectType>
            <supportedProjectType>war</supportedProjectType>
        </supportedProjectTypes>
        <instructions>
            <Built-By>${project.organization.name}</Built-By>
            <Bundle-Vendor>${project.organization.name}</Bundle-Vendor>
            <Bundle-ContactAddress>${project.organization.url}</Bundle-ContactAddress>
            <Bundle-Description>${project.description}</Bundle-Description>
            <Bundle-DocURL>${bundle.doc.url}</Bundle-DocURL>
            <Bundle-Category>${bundle.category}</Bundle-Category>
            <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
            <Bundle-Version>${project.version}</Bundle-Version>

            <Import-Package>*</Import-Package>
            <Export-Package>*</Export-Package>
        </instructions>
    </configuration>
    <executions>
        <execution>
            <id>bundle</id>
            <goals>
                <goal>manifest</goal>
            </goals>
            <phase>prepare-package</phase>
            <inherited>true</inherited>
        </execution>
    </executions>
</plugin>

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.3.1</version>
    <configuration>
        <archive>
            <manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
        </archive>
    </configuration>
</plugin>

<plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.1.1</version>
    <executions>
        <execution>
            <id>create-war</id>
            <phase>package</phase>
            <goals>
                <goal>war</goal>
            </goals>
            <inherited>true</inherited>
        </execution>
    </executions>
    <configuration>
        <archive>
            <manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
        </archive>
    </configuration>
</plugin>
</pluginManagement>
Run Code Online (Sandbox Code Playgroud)

然后在孩子POM中,你可以简单地做:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
        </plugin>
        <!-- Below is mutually exclusive: Either jar or war plugin -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
        </plugin>
    </plugins>
</build>
Run Code Online (Sandbox Code Playgroud)


Pet*_*ens 5

看看 bndtools,它对创建打包包的项目有很好的支持。它提供了很多关于 JAR 的结构以及它们如何依赖于其他事物的见解。