maven - 使用 maven-assembly-plugin 生成 OSGi 包分发

标签 maven osgi distribution maven-assembly-plugin maven-resources-plugin

我有一个多模块项目,其中每个模块都使用 Apache Felix maven-bundle-plugin 打包为一个 OSGi 包。整个项目是使用列出上述模块的父 POM 构建的。某些模块包含配置资源(例如 .properties 文件),不应将其放入 bundle 中进行部署,而应将其外部化在专用的配置文件夹中。我的目标是创建一个如下所示的分发文件夹(可能是一个 zip 文件):

my-app-distribution
    /bundles
        module1-bundle.jar
        module2-bundle.jar
        etc.

    /conf
        external1.properties
        external2.properties
        etc.

/conf 目录下的属性文件是从各个模块的 /target 文件夹中精心挑选的文件。 .properties 文件需要从目标文件夹而不是 src 文件夹中获取的原因是我正在使用 Maven 资源过滤,并且 source 属性文件包含 ${..} 环境特定值的占位符。这些占位符在构建过程中被正确解析 - 每个构建配置文件 - 并且 target/ 文件夹包含实际的特定于环境的值。

我已经做过很多次这样的分发文件操作 - 对于带有可执行 JAR 的分发等。在这种情况下,我想使用程序集描述符的“moduleSets”配置 - 很容易将所有二进制文件/jar 拉到一个使用 moduleSet/binary 描述符的单个分发文件夹。在 maven-bundle-plugin 中也很容易将某些文件排除在打包到 OSGi 包中之外。我遇到的唯一问题是创建 /conf 分发文件夹并在那里收集必要的属性文件。我试图在“moduleSet/sources”描述符中使用“fileSets”来仅包含来自每个模块的 **/target 的特定文件,但这似乎不起作用。

有人有建议吗?必须有一个简单的方法。或者我根本不应该使用?

谢谢,

简历


@PetrKozelka 我不确定将特定于不同包的配置文件提取到单独的模块中是个好主意。 OSGi 的全部要点是 bundle 是独立的并且可能是可重用的——在开发和分发中都是如此。只有在源代码中将功能实现和相关配置文件组合在一起才有意义。对于特定的发行版,我可能需要提取一些文件——如果管理员需要控制某些参数。对于不同的发行版/应用程序,这可能会有所不同。程序集配置可能会更改,但包/源将保持不变。此外,每个 bundle 都可能单独开发和使用,并非所有 bundle 都必须始终属于同一个 super 项目——正如您所假设的那样。您的建议似乎属于按 Artifact 类型(例如“模型”、“服务”、“数据访问”、“配置”等)打包企业应用程序的旧类别,而不是按功能域/功能。这种方法在单个应用程序/项目中工作正常,但在企业级别上失败,因为企业级别通常需要重用垂直组件的子集(按功能域划分)。

对于您依赖于模块中的文件布局的观点,我同意不应该存在这种依赖性。可以根据非常具体的发行版要求,通过明确的名称或命名约定来手动挑选文件。 (这正是我面临的情况。)

最佳答案

我实际上已经想出了如何或多或少优雅地做到这一点。在下面发布解决方案,以防其他人正在寻找解决类似问题的方法。

总结

我正在使用 maven-assembly-plugin从各个模块中提取二进制文件(捆绑 JAR)并将它们打包在 <my-distribution-folder>/bundles 中目录。在每个应该外部化一些资源文件的模块中,我将这些文件合并到 /src/main/resources/external 下目录,并使用 maven-resources-plugin在打包阶段将这些资源复制到我专用的自动生成目录 distribution包含 assembly.xml 的模块描述 rune 件,也作为顶级项目的一部分构建。我用 maven-clean-plugin在父 POM 中,在顶级项目构建的 CLEAN 阶段清除分发暂存目录的内容。

MAVEN 配置

在包含需要外部化资源的每个包的模块 POM 中,我添加了以下资源管理配置:

 <build>
    <defaultGoal>install</defaultGoal>

    <!--
    enable resource filtering for resolving ${...} placeholders with environment-specific values 
    exclude any files that must be externalized
    -->
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <excludes>
                <exclude>external/*.*</exclude>
            </excludes>
        </resource>
    </resources>
    ...

   <plugins>
        <!-- Copies contents of resources/external to dedicated folder defined by property in parent -->
        <!-- externalized resources will be packaged according to assembly instructions -->
        <plugin>
           <artifactId>maven-resources-plugin</artifactId>
           <version>2.6</version>
           <executions>
               <execution>
                   <id>copy-resources</id>
                   <phase>package</phase>
                   <goals>
                       <goal>copy-resources</goal>
                   </goals>
                   <configuration>
                       <outputDirectory>
                           ${project.parent.basedir}/${externalizableResourcesStageDir}
                       </outputDirectory>
                       <resources>
                           <resource>
                               <directory>src/main/resources/external</directory>
                               <filtering>true</filtering>
                           </resource>
                       </resources>
                   </configuration>
               </execution>
            </executions>
       </plugin>

        <!-- builds a JAR file for this bundle -->
        <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <extensions>true</extensions>
            <configuration>
                <instructions>
                    <Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
                    <Import-Package>*</Import-Package>
                    <Export-Package>
                        ${project.groupId}.thismodulepackage*;version=${project.version}
                    </Export-Package>
                </instructions>
            </configuration>
        </plugin>
    </plugins>
</build>

哪里externalizableResourcesStageDir是在顶层/父 POM 中定义的属性。在项目中,我包含了一个具有以下结构的特殊分发模块:

distribution
   /ext-resources  (target auto-generated dir for external resources from modules)   
   /src
       /assemble
             assembly.xml   (assembly descriptor)

assembly.xml文件看起来像这样:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2
      http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>bin</id>

<!-- generate a ZIP distribution -->
<formats>
    <format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<baseDirectory>/</baseDirectory>

<moduleSets>
    <moduleSet>
        <!-- Enable access to all projects in the current multi-module build -->
        <useAllReactorProjects>true</useAllReactorProjects>

        <!-- select projects to include-->
        <includes>
            <include>myGroupId:myModuleArtifactId1</include>
            <include>myGroupId:myModuleArtifactId2</include>
            ...
        </includes>

        <!-- place bundle jars under /bundles folder in dist directory -->
        <binaries>
            <outputDirectory>${artifactId}/bundles</outputDirectory>
            <unpack>false</unpack>
        </binaries>
    </moduleSet>
</moduleSets>

<!-- now take files from ext-resources in this module and place them into dist /conf subfolder-->
<fileSets>
    <fileSet>
        <directory>ext-resources</directory>
        <outputDirectory>${artifactId}/conf/</outputDirectory>
        <includes>
            <include>*</include>
        </includes>
    </fileSet>

</fileSets>

</assembly>

分发模块的 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>

<parent>
    <groupId>myGroupId</groupId>
    <artifactId>parentArtifactId</artifactId>
    <version>...</version>
</parent>

<groupId>myGroupId</groupId>
<artifactId>distribution</artifactId>
<version>...</version>

<packaging>pom</packaging>
<name>Distribution</name>
<description>This module creates the <MyProject> Distribution Assembly</description>
<url>http:...</url>

<!-- NOTE: These dependency declarations are only required to sort this project to the
     end of the line in the multi-module build.
-->
<dependencies>
    <dependency>
        <groupId>myGroupId</groupId>
        <artifactId>myModuleArtifactId1</artifactId>
        <version>${project.version}</version>
    </dependency>
 ...
</dependencies>

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <id>dist-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <descriptors>
                            <descriptor>src/assemble/assembly.xml</descriptor>
                        </descriptors>
                    </configuration>
                </execution>
            </executions>
        </plugin>

    </plugins>
</build>
</project>

父 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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>myGroupId</groupId>
    <artifactId>myParentId</artifactId>
    <version>...</version>
    <packaging>pom</packaging>

    <properties>
        ...

        <!-- directory where build may place any sub-modules' resources that should be externalized -->
        <!-- those resources may be picked up by maven-assembly-plugin and packaged properly for distribution -->
        <externalizableResourcesStageDir>
            esb-distribution/ext-resources
        </externalizableResourcesStageDir>

    </properties>

    <!-- all project modules (OSGi bundles + distribution) -->
    <modules>
        <module>bundle-module1</module>
        <module>bundle-module2</module>
        ...
        <module>distribution</module>
    </modules>



    <dependencyManagement>
      <dependencies>
      ...
      </dependencies>
    </dependencyManagement>

    <build>
        <pluginManagement>
            <plugins>
                <!--
                Cleans contents of the folder where the externalized resources will be consolidated
                Each module adds its own external files to the distribution directory during its own build
                -->
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>2.5</version>
                    <executions>
                        <execution>
                            <id>clean-ext-resources</id>
                            <phase>clean</phase>
                        </execution>
                    </executions>
                    <configuration>
                        <filesets>
                            <fileset>
                                <directory>${externalizableResourcesStageDir}</directory>
                                <includes>
                                    <include>*.*</include>
                                </includes>
                                <followSymlinks>false</followSymlinks>
                            </fileset>
                        </filesets>
                    </configuration>
                </plugin>


                <plugin>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <version>2.3</version>
                    <configuration>
                        <descriptors>
                            <descriptor>src/assemble/assembly.xml</descriptor>
                        </descriptors>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

</project>

注意:我们还确保将外部化资源文件排除在打包到单独的包 JAR 中(参见模块 POM 的 resources 部分。)生成的解压缩分发将如下所示:

my-app-distribution
  /bundles
    module1-bundle.jar
    module2-bundle.jar
    etc.

  /conf
    external1.properties
    external2.properties
    etc. 

关于maven - 使用 maven-assembly-plugin 生成 OSGi 包分发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13203566/

相关文章:

algorithm - 一个均匀有序的分布问题

java - Maven 不会因编译器警告而失败

java - UnsupportedClassVersionError : has been compiled by a more recent version of the Java Runtime (class file version 55. 0),此版本 (..) 最高为 52.0

maven - Gradle 告诉我 ' Must specify a repository for deployment'

oracle - 如何在 osgi Liferay DXP 中添加第三方依赖

eclipse - 创建 Eclipse "Distribution"?

java - 如何编译基于 Maven 的 Java 项目以从命令行运行它

java - Aries NamespaceHandler 获取 Bean 的引用

java - 使用geojson库时如何修复 "the-default-package-is-not-permitted-by-the-import-package-syntax"错误?

linux - 编写我自己的内核