Error creating assembly archive in multi module maven project

Karthik picture Karthik · Oct 23, 2017 · Viewed 11.5k times · Source

I have created a multi-module maven project with 2 modules(child1, child2).
I am trying to create a package - APP.zip which should contain app.properties, child1.war and child2.war by using assembly plugin.
But I am getting the below error when I run mvn assembly:single command

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-assembly-plugin:2.6:single (default-cli) on project app: Failed to create assembly: Error creating assembly archive My_Package: You must set at least one file. -> [Help 1]

Project folder structure

app
  |
   pom.xml
   assembly.xml
   app.properties 
   child 1
       | 
       pom.xml src target
                   |
                     child1.war
   child 2
       |
       pom.xml src target
                   |
                     child2.war   

Parent pom (app/pom.xml)

    <project>
        <groupId>com.karthik</groupId>
        <artifactId>parent</artifactId>
        <packaging>pom</packaging>
        <version>1.0-SNAPSHOT</version>
        <name>parent</name>

        <modules>
           <module>child 1</module>
           <module>child 2</module>
        </modules>
        <build>
           <pluginManagement>
               <plugins>
                  <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <finalName>APP</finalName>
                    <appendAssemblyId>false</appendAssemblyId>
                    <descriptors>
                    <descriptor>assembly.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                     <execution>
                    <id>make-bundles</id>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <phase>package</phase>
                      </execution>
                </executions>
            </plugin>
        </plugins>
  </pluginManagement>
</build>
</project>

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">
    <formats>
        <format>zip</format>
    </formats>
    <id>My_Package</id>
    <includeBaseDirectory>false</includeBaseDirectory>
    <moduleSets>
        <moduleSet>
            <useAllReactorProjects>true</useAllReactorProjects>
            <includes>
                <include>com.karthik:child1</include>
                <include>com.karthik:child2</include>
            </includes>
        </moduleSet>
    </moduleSets>
</assembly>

I believe I have missed something in the assembly descriptor that is causing this error.
1) Can you please help to resolve this issue & help creating a zip(APP.zip) containing app.properties, child1.war and child2.war?
2) When do we need to use <fileSet> & <moduleSet> tags?

Answer

Karthik picture Karthik · Oct 25, 2017

Finally I found a solution

1) Add a separate module - distribution for assembly in parent POM.

Parent POM

<project>
        <groupId>com.karthik</groupId>
        <artifactId>parent</artifactId>
        <packaging>pom</packaging>
        <version>1.0-SNAPSHOT</version>
        <name>parent</name>

        <modules>
           <module>child 1</module>
           <module>child 2</module>
           <module>distribution</module>
        </modules>
</project>

2) Configure maven assembly plugin in distribution module

Distribution module POM

    <parent>
        <groupId>com.karthik</groupId>
        <artifactId>parent</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>distribution</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>Distribution</name>

    <build>
        <plugins>
            <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                 <execution>
                <id>make-bundles</id>
                <goals>
                    <goal>single</goal>
                </goals>
                <phase>package</phase>
                <configuration>
                       <descriptors>
                        <descriptor>assembly.xml</descriptor>
                    </descriptors>
                </configuration>
            </execution>
        </executions>
    </plugin>
  </plugins>
</build>

3) Add files/artifacts to be assembled in <files> element of assembly descriptor

assembly.xml

<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>distribution</id>
  <formats>
      <format>zip</format>
  </formats>
  <files>
        <file>
            <source>../Child1/target/child1.war</source>
        </file>
        <file>
            <source>../Child2/target/child2.war</source>
        </file>
        <file>
            <source>../app.properties</source>
        </file>
    </files>
</assembly>