Search code examples
mavenmaven-assembly-plugin

Copy file in maven assembly


I am building one maven assembly. The structure is like this:

src
  main
    server
      app1
      app2
      app3
    client
      app1
      app2
      app3
    common
      server.xml

The assembly makes the output as:

server.zip
  app1
  app2
  app3

I want to include server.xml in each of the output folders for apps. So my output should be:

server.zip
  app1
    server.xml  
  app2
    server.xml  
  app3
    server.xml

How can I do that?


Solution

  • You need to copy the file server.xml multiple times, by declaring it multiple times in the assembly.xml. Here's a sample assembly descriptor:

    <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
        <id>server</id>
        <formats>
            <format>zip</format>
        </formats>
        <fileSets>
            <fileSet>
                <directory>src/main/server/app1</directory>
                <outputDirectory>app1</outputDirectory>
            </fileSet>
        </fileSets>
        <files>
            <file>
                <source>src/main/common/server.xml</source>
                <outputDirectory>app1</outputDirectory>
            </file>
        </files>
        <!-- same configuration for app2 and 3 -->
    </assembly>
    

    If you want to do this in a loop (let's say you have 50 app folders), it is possible to generate the correct Maven assembly descriptor using a Velocity template. Invoking Velocity is done with the velocity-maven-plugin.

    Sample velocity template (save as src/main/common/assembly-server.xml.vm):

    <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
        <id>server</id>
        <formats>
            <format>zip</format>
        </formats>
        <fileSets>
            #foreach($i in [1..50])
            <fileSet>
                <directory>src/main/server/app$i</directory>
                <outputDirectory>app$i</outputDirectory>
            </fileSet>
            #end
        </fileSets>
        <files>
            #foreach($i in [1..50])
            <file>
                <source>src/main/common/server.xml</source>
                <outputDirectory>app$i</outputDirectory>
            </file>
            #end
        </files>
    </assembly>
    

    Plugin configuration:

    <plugin>
        <groupId>com.googlecode.velocity-maven-plugin</groupId>
        <artifactId>velocity-maven-plugin</artifactId>
        <version>1.1.0</version>
        <executions>
            <execution>
                <id>generate-server-assembly<id>
                <phase>generate-resources</phase>
                <goals>
                    <goal>velocity</goal>
                <goals>
            <execution>
        </executions>
        <configuration>
            <templateFiles>
                <directory>/src/main/common</directory>
                <includes>
                    <include>assembly-server.xml.vm</include>
                </includes>
            </templateFiles>
            <removeExtension>.vm</removeExtension>
        </configuration>
    </plugin>
    

    This will generate a assembly-server.xml file under target that you can use as a assembly descriptor for the maven-assembly-plugin.