How can I merge resource files in a Maven assembly?

前端 未结 5 986
闹比i
闹比i 2020-12-06 10:09

I\'m using Maven and its assembly plugin to build a distribution package of my project like this:

  • one project assembles a basic runtime (based on Felix), with
5条回答
  •  臣服心动
    2020-12-06 10:55

    Expanding a bit on Juergen's answer for those who stumble on this - the containerDescriptorHandler in the descriptor can take four values (v2.3), these are metaInf-services, file-aggregator, plexus, metaInf-spring. It's a bit buried in the code (found in the package org.apache.maven.plugin.assembly.filter) but it is possible to aggregate config/properties files.

    Here's an example descriptor that aggregates the META-INF/services and named property files located in com.mycompany.actions.

    descriptor.xml

    
    
    ...
    
    
    
        
            metaInf-services
        
    
        
            file-aggregator
            
                com/mycompany/actions/action.properties
                com/mycompany/actions/action.properties
            
        
    
    
    
    ....
    
    
    

    The file-aggregator can contain a regular expression in the filePattern to match multiple files. The following would match all files names 'action.properties'.

    .+/action.properties
    

    The metaInf-services and metaInf-spring are used for aggregating SPI and spring config files respectively whilst the plexus handler will aggregate META-INF/plexus/components.xml together.

    If you need something more specialised you can add your own configuration handler by implementing ContainerDescriptorHandler and defining the component in META-INF/plexus/components.xml. You can do this by creating an upstream project which has a dependency on maven-assembly-plugin and contains your custom handler. It might be possible to do this in the same project you're assembling but I didn't try that. Implementations of the handlers can be found in org.apache.maven.plugin.assembly.filter.* package of the assembly source code.

    CustomHandler.java

    package com.mycompany;
    
    import org.apache.maven.plugin.assembly.filter.ContainerDescriptorHandler;
    
    public class CustomHandler implements ContainerDescriptorHandler {
        // body not shown
    }
    

    then define the component in /src/main/resources/META-INF/plexus/components.xml

    components.xml

    
    
        
            
                org.apache.maven.plugin.assembly.filter.ContainerDescriptorHandler
                custom-handler
                com.mycompany.CustomHandler
                per-lookup
            
        
    
    

    Finally you add this as a dependency on the assembly plugin in the project you wish to assemble

    pom.xml

    
        org.apache.maven.plugins
        maven-assembly-plugin
        2.2.1
        
            
                ...
            
        
        
            
                com.mycompany
                sample-handler
                1.0
            
        
    
    

    and define the handlerName in the descriptor

    descriptor.xml

    ...
    
        custom-handler
    
    ...
    

    The maven-shade-plugin can also create 'uber-jars' and has some resource transforms for handling XML, licences and manifests.

    J

提交回复
热议问题