Maven 2 如何在Maven程序集中合并资源文件?
我正在使用Maven及其汇编插件构建我的项目的分发包,如下所示:Maven 2 如何在Maven程序集中合并资源文件?,maven-2,assemblies,build-automation,Maven 2,Assemblies,Build Automation,我正在使用Maven及其汇编插件构建我的项目的分发包,如下所示: 一个项目将一个基本运行时(基于Felix)和适当的目录和包组装在一个ZIP文件中 第三方库分别收集在一个项目中,或者转换为OSGi捆绑包,或者,如果它们已经与OSGi兼容,则只是复制 我自己的项目也包含几个内置到OSGi包中的模块 现在,我正在添加另一个项目,该项目将解压ZIP,将所有其他jar放到适当的目录中,并重新打包以供分发。现在,我的bundle可能包含我想要合并到运行时程序集中相同名称的配置文件,而不是替换这些配置文
- 一个项目将一个基本运行时(基于Felix)和适当的目录和包组装在一个ZIP文件中
- 第三方库分别收集在一个项目中,或者转换为OSGi捆绑包,或者,如果它们已经与OSGi兼容,则只是复制
- 我自己的项目也包含几个内置到OSGi包中的模块
这些文件是纯文本(属性文件),但我以后可能会遇到与XML文件类似的情况。我不知道有什么可靠的解决方案可以解决这个问题。但是环顾四周就会发现,必须有人合并属性文件。从外观上看,您需要告诉它要合并哪些文件,这是一件好事,因为您不希望随意应用这些文件 假设您已使用dependency unpack将zip解压缩到已知位置,则需要配置插件以合并每对属性文件并指定适当的目标位置
您可以使用类似于EL4J中的xmlmerge的东西来扩展插件以处理XML,如中所述。旧问题,但在尝试解决类似问题时遇到了它:汇编插件2.2具有合并文件的功能:
e、 g.handlerName“metaInf服务”(将包含所有META-INF/services文件),“metaInf spring”是我唯一知道的(我个人需要metaInf服务)我还创建了一个合并文件插件,在我的例子中,我使用它将来自不同项目的SQL文件合并到一个安装程序SQL文件中,该文件可以在一个文件中为我们的应用程序创建所有模式/表/静态数据等,为那些偶然发现这一点的人扩展一下Juergen的答案-描述符中的
containerDescriptorHandler
可以取四个值(v2.3),这些是metaInf服务
,文件聚合器
,丛
,metaInf spring
。它有点隐藏在代码中(可以在包org.apache.maven.plugin.assembly.filter
中找到),但是可以聚合配置/属性文件
下面是一个聚合META-INF/services
和
位于com.mycompany.actions
中的命名属性文件
descriptor.xml
<assembly>
...
<containerDescriptorHandlers>
<containerDescriptorHandler>
<handlerName>metaInf-services</handlerName>
</containerDescriptorHandler>
<containerDescriptorHandler>
<handlerName>file-aggregator</handlerName>
<configuration>
<filePattern>com/mycompany/actions/action.properties</filePattern>
<outputPath>com/mycompany/actions/action.properties</outputPath>
</configuration>
</containerDescriptorHandler>
</containerDescriptorHandlers>
....
</assembly>
<?xml version='1.0' encoding='UTF-8'?>
<component-set>
<components>
<component>
<role>org.apache.maven.plugin.assembly.filter.ContainerDescriptorHandler</role>
<role-hint>custom-handler</role-hint>
<implementation>com.mycompany.CustomHandler</implementation>
<instantiation-strategy>per-lookup</instantiation-strategy>
</component>
</components>
</component-set>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.1</version>
<configuration>
<descriptors>
<descriptor>...</descriptor>
</descriptors>
</configuration>
<dependencies>
<dependency>
<groupId>com.mycompany</groupId>
<artifactId>sample-handler</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</plugin>
...
<containerDescriptorHandler>
<handlerName>custom-handler</handlerName>
</containerDescriptorHandler>
...
META-INF服务
和META-INF-spring
分别用于聚合和spring配置文件,而plexus
处理程序将聚合META-INF/plexus/components.xml
如果您需要更专业的配置处理程序,您可以通过实现ContainerDescriptorHandler
并在META-INF/plexus/components.xml
中定义组件来添加自己的配置处理程序。您可以通过创建一个上游项目来实现这一点,该项目依赖于maven assembly plugin
,并包含您的自定义处理程序。在您正在组装的同一个项目中可能会这样做,但我没有尝试过。处理程序的实现可以在程序集源代码的org.apache.maven.plugin.assembly.filter.*
包中找到
CustomHandler.java
package com.mycompany;
import org.apache.maven.plugin.assembly.filter.ContainerDescriptorHandler;
public class CustomHandler implements ContainerDescriptorHandler {
// body not shown
}
然后在/src/main/resources/META-INF/plexus/components.xml
components.xml
<assembly>
...
<containerDescriptorHandlers>
<containerDescriptorHandler>
<handlerName>metaInf-services</handlerName>
</containerDescriptorHandler>
<containerDescriptorHandler>
<handlerName>file-aggregator</handlerName>
<configuration>
<filePattern>com/mycompany/actions/action.properties</filePattern>
<outputPath>com/mycompany/actions/action.properties</outputPath>
</configuration>
</containerDescriptorHandler>
</containerDescriptorHandlers>
....
</assembly>
<?xml version='1.0' encoding='UTF-8'?>
<component-set>
<components>
<component>
<role>org.apache.maven.plugin.assembly.filter.ContainerDescriptorHandler</role>
<role-hint>custom-handler</role-hint>
<implementation>com.mycompany.CustomHandler</implementation>
<instantiation-strategy>per-lookup</instantiation-strategy>
</component>
</components>
</component-set>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.1</version>
<configuration>
<descriptors>
<descriptor>...</descriptor>
</descriptors>
</configuration>
<dependencies>
<dependency>
<groupId>com.mycompany</groupId>
<artifactId>sample-handler</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</plugin>
...
<containerDescriptorHandler>
<handlerName>custom-handler</handlerName>
</containerDescriptorHandler>
...
org.apache.maven.plugin.assembly.filter.ContainerDescriptorHandler
自定义处理程序
com.mycompany.CustomHandler
每次查找
最后,将其作为对要组装的项目中的组装插件的依赖项添加
pom.xml
<assembly>
...
<containerDescriptorHandlers>
<containerDescriptorHandler>
<handlerName>metaInf-services</handlerName>
</containerDescriptorHandler>
<containerDescriptorHandler>
<handlerName>file-aggregator</handlerName>
<configuration>
<filePattern>com/mycompany/actions/action.properties</filePattern>
<outputPath>com/mycompany/actions/action.properties</outputPath>
</configuration>
</containerDescriptorHandler>
</containerDescriptorHandlers>
....
</assembly>
<?xml version='1.0' encoding='UTF-8'?>
<component-set>
<components>
<component>
<role>org.apache.maven.plugin.assembly.filter.ContainerDescriptorHandler</role>
<role-hint>custom-handler</role-hint>
<implementation>com.mycompany.CustomHandler</implementation>
<instantiation-strategy>per-lookup</instantiation-strategy>
</component>
</components>
</component-set>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.1</version>
<configuration>
<descriptors>
<descriptor>...</descriptor>
</descriptors>
</configuration>
<dependencies>
<dependency>
<groupId>com.mycompany</groupId>
<artifactId>sample-handler</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</plugin>
...
<containerDescriptorHandler>
<handlerName>custom-handler</handlerName>
</containerDescriptorHandler>
...
org.apache.maven.plugins
还可以创建“uber JAR”,并具有一些用于处理XML、许可证和清单的资源转换
J(在maven central上提供)似乎也能完成这项工作。
请参见下面的配置示例
<plugin>
<groupId>org.zcore.maven</groupId>
<artifactId>merge-maven-plugin</artifactId>
<version>0.0.3</version>
<executions>
<execution>
<id>merge</id>
<phase>generate-resources</phase>
<goals>
<goal>merge</goal>
</goals>
<configuration>
<mergers>
<merger>
<target>${build.outputDirectory}/output-file-1</target>
<sources>
<source>src/main/resources/file1</source>
<source>src/main/resources/file2</source>
</sources>
</merger>
<merger>
<target>${build.outputDirectory}/output-file-2</target>
<sources>
<source>src/main/resources/file3</source>
<source>src/main/resources/file4</source>
</sources>
</merger>
</mergers>
</configuration>
</execution>
</executions>
org.zcore.maven
合并maven插件
0.0.3
合并
产生资源
合并
${build.outputDirectory}/output-file-1
src/main/resources/file1
src/main/resources/file2
${build.outputDirectory}/output-file-2
src/main/resources/file3
src/main/resources/file4
我不知道有什么能做到这一点。也就是说,我不太习惯OSGI。然而,这是一个“常规”用例吗?@Pascal-这并不是OSGi特有的,真的。我不确定这有多正常,但我可以想象,如果我想合并WARs或OSGi捆绑包,我可能会分别合并web.xml或MANIFEST.MF文件。我肯定还有其他相同类型的任务,但可能有一种完全不同的方法。为什么要合并OSGI包?我对OSGI很在行,但他们不是独立的单位吗?对于战争,它有一点更清楚,但是我可以想到合并的许多问题(例如,如果两个战争都有一个<代码>索引。JSP ,如果它们都依赖同一个LIB,但不同版本等等),也会认为它们是独立的单位。修补web.xml
似乎更“现实”(并且可能很有用,例如在测试环境中,cargo有一个目标)。@Pascal-不要