如何合并Maven程序集中的资源文件?

Han*_*etz 13 build-automation maven-2 assemblies

我正在使用Maven及其程序集插件来构建我的项目的分发包,如下所示:

  • 一个项目在ZIP文件中组装一个基本的运行时(基于Felix),以及相应的目录和包.
  • 第三方库分别收集在一个项目中,并转换为OSGi包,如果它们已经兼容OSGi,则只需复制它们
  • 我自己的项目也包含几个内置于OSGi包中的模块.

现在,我正在添加另一个解压缩ZIP的项目,将所有其他JAR放入正确的目录中,然后重新打包以进行分发.现在,我的bundle可能包含我要合并到的配置文件,而不是替换运行时程序集中名称相同的配置文件.我怎么做?

这些文件是纯文本(属性文件),但稍后我可能会遇到与XML文件类似的情况.

jwm*_*may 24

在克林斯曼的回答对于那些谁在这个跌跌扩展一点-的containerDescriptorHandler描述符可以采取四个值(V2.3),这些都是metaInf-services,file-aggregator,plexus,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>
Run Code Online (Sandbox Code Playgroud)

file-aggregator可包含一个正则表达式的filePattern匹配多个文件.以下内容将匹配所有文件名'action.properties'.

<filePattern>.+/action.properties</filePattern>
Run Code Online (Sandbox Code Playgroud)

metaInf-servicesmetaInf-spring用于聚集SPI分别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
}
Run Code Online (Sandbox Code Playgroud)

然后定义组件 /src/main/resources/META-INF/plexus/components.xml

components.xml中

<?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>
Run Code Online (Sandbox Code Playgroud)

最后,将此添加为您要组装的项目中的程序集插件的依赖项

的pom.xml

<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>
Run Code Online (Sandbox Code Playgroud)

并在描述符中定义handlerName

descriptor.xml

...
<containerDescriptorHandler>
    <handlerName>custom-handler</handlerName>
</containerDescriptorHandler>
...
Run Code Online (Sandbox Code Playgroud)

行家遮阳帘插件还可以创建"尤伯杯罐子",并具有用于处理XML,许可证和舱单一些资源转变.

Ĵ


小智 9

老问题但在尝试解决类似问题时偶然发现它:Assembly plugin 2.2具有合并文件的功能:http ://maven.apache.org/plugins/maven-assembly-plugin/assembly.html#class_containerDescriptorHandler eg handlerName"metaInf-服务"(将连接所有META-INF /服务文件),"metaInf-spring"是我所知道的唯一(我个人需要metaInf服务)


Ric*_*ler 6

我不知道这个问题的可靠解决方案.但是有点环顾四周表明有人创建了一个合并属性文件的插件.从它的外观来看,你需要告诉它要合并哪些文件,这是一件好事,因为你不希望这个应用程序illy nilly.

假设您已使用dependency-unpack将zip解压缩到已知位置,则可以配置插件以合并每对属性文件并指定适当的目标位置.

您可以使用EL4J中的xmlmerge之类的东西来扩展插件以处理XML,如本Javaworld 文章中所述.

  • 该插件实际上是由我制作的.很高兴看到它对某人有用. (2认同)
  • 链接又坏了 (2认同)