cvn*_*new 3 osgi distribution maven maven-assembly-plugin maven-resources-plugin
我有一个多模块项目,其中每个模块使用Apache Felix maven-bundle-plugin打包为OSGi包.整个项目使用父POM构建,列出上述模块.某些模块包含配置资源(例如.properties文件),这些资源不应该在捆绑包内用于部署,而是在专用配置文件夹中外部化.我的目标是创建一个分发文件夹(可能是一个zip文件),看起来像这样:
my-app-distribution
/bundles
module1-bundle.jar
module2-bundle.jar
etc.
/conf
external1.properties
external2.properties
etc.
Run Code Online (Sandbox Code Playgroud)
目录下的属性文件/conf是从各个模块的/target文件夹中手工挑选的文件.究其原因,.properties文件需要从目标文件夹与在src文件夹拾起的是,我使用Maven资源过滤,和源属性文件包含${..}了特定环境下的值的占位符.这些占位符在构建过程中正确解析 - 每个构建配置文件 - 并且target/文件夹包含实际的特定于环境的值.
我已经多次完成了这样的分发文件操作 - 对于具有可执行JAR的发行版等.在这种情况下,我想使用程序集描述符的"moduleSets"配置 - 很容易将所有二进制文件/ jar放入单个分发文件夹中使用moduleSet /二进制描述符.在maven-bundle-plugin中也很容易将某些文件排除在打包到OSGi包中.我遇到的唯一问题是创建/conf分发文件夹并在那里收集必要的属性文件.我试图在"moduleSet/sources"描述符中使用"fileSets"来仅包含**/target每个模块的特定文件,但这似乎不起作用.
有人有建议吗?必须有一个简单的方法.或者我根本不应该使用?
谢谢,
简历
@PetrKozelka我不确定将特定于不同软件包的配置文件提取到一个单独的模块中是个好主意.OSGi的重点在于捆绑包是独立的,并且可以在开发和分发中重复使用.只有在源代码中将功能实现和相关配置文件组合在一起才有意义.对于特定的发行版,虽然我可能需要提取一些文件 - 如果管理员需要控制某些参数.对于不同的分发/应用,这可能是不同的.装配配置可能会更改,但捆绑/源将保持不变.此外,每个捆绑包可能会被单独开发和使用,并非所有捆绑包都必须始终是同一个超级项目的一部分 - 正如您所假设的那样.工件的类型(例如"模型","服务","数据访问","配置"等),而不是功能域/特征.这种方法在单个应用程序/项目中正常工作,但在企业级别上失败,因为通常需要重用垂直组件的子集(按功能域划分).
至于你依赖于模块中的文件布局,我同意不应该有这样的依赖.文件可以通过其显式名称或命名约定来手工挑选 - 根据非常具体的发行版要求.(这正是我面临的情况.)
我实际上已经想出了如何或多或少地优雅地做到这一点.如果其他人正在寻求解决类似的问题,请在下面发布解决方案.
摘要
我正在使用它maven-assembly-plugin从各个模块中提取二进制文件(捆绑JAR)并将它们打包到<my-distribution-folder>/bundles目录中.在应该将一些资源文件外部化的每个模块中,我将这些文件合并到/src/main/resources/external目录下,并用于maven-resources-plugin在打包阶段将这些资源复制到distribution包含assembly.xml描述符文件的专用模块中的自动生成目录,并且也构建为部分顶级项目.我maven-clean-plugin在父POM中使用在顶级项目构建的CLEAN阶段清除分发临时目录的内容.
MAVEN CONFIGURATION
在每个包的模块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>
Run Code Online (Sandbox Code Playgroud)
where externalizableResourcesStageDir是在顶级/父级POM中定义的属性.在项目中,我包含一个具有以下结构的特殊分发模块:
distribution
/ext-resources (target auto-generated dir for external resources from modules)
/src
/assemble
assembly.xml (assembly descriptor)
Run Code Online (Sandbox Code Playgroud)
该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>
Run Code Online (Sandbox Code Playgroud)
分发模块的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>
Run Code Online (Sandbox Code Playgroud)
父POM将列出所有bundle模块,以及分发模块,并定义程序集插件:
<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>
Run Code Online (Sandbox Code Playgroud)
注意:我们还确保将外部化资源文件排除在单个包JAR中的打包之外(请参阅resources模块POM 的部分.)生成的解压缩分发将如下所示:
my-app-distribution
/bundles
module1-bundle.jar
module2-bundle.jar
etc.
/conf
external1.properties
external2.properties
etc.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2309 次 |
| 最近记录: |