3

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?

Karthik
  • 1,302
  • 5
  • 25
  • 56

3 Answers3

4

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>
Karthik
  • 1,302
  • 5
  • 25
  • 56
  • A separate module just for the assembly seems to be the only solution. There is a FAQ entry which explains the underlying situation: https://maven.apache.org/plugins/maven-assembly-plugin/faq.html#module-binaries – Falco Preiseni Sep 16 '20 at 09:15
0

Use the example here

This is a working example, parent pom:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.greg</groupId>
  <artifactId>assembly-example</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>

  <name>assembly-example</name>

  <modules>
    <module>child1</module>
    <module>child2</module>
    <module>distribution</module>
  </modules>

  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>3.1.0</version>
          <configuration>
            <descriptors>
              <descriptor>src/assembly/assembly.xml</descriptor>
            </descriptors>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

</project>

Child pom:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>com.greg</groupId>
    <artifactId>assembly-example</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>

  <artifactId>child1</artifactId>
  <packaging>jar</packaging>

  <name>child1</name>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

distribution pom:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>com.greg</groupId>
    <artifactId>assembly-example</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>

  <artifactId>distribution</artifactId>
  <packaging>jar</packaging>

  <name>Distribution</name>

  <dependencies>
    <dependency>
      <groupId>com.greg</groupId>
      <artifactId>child1</artifactId>
      <version>${project.version}</version>
    </dependency>
    <dependency>
      <groupId>com.greg</groupId>
      <artifactId>child2</artifactId>
      <version>${project.version}</version>
    </dependency>
  </dependencies>

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

assembly.xml in the distribution/src/assembly directory :

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">

  <id>bin</id>
  <formats>
    <format>zip</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <moduleSets>
    <moduleSet>
      <useAllReactorProjects>true</useAllReactorProjects>
      <includes>
        <include>com.greg:child1</include>
      </includes>
      <binaries>
        <outputDirectory>modules/maven-assembly-plugin</outputDirectory>
        <unpack>false</unpack>
      </binaries>
    </moduleSet>
  </moduleSets>
</assembly>

mvn clean package p

Produces a zip file:

jar tvf distribution/target/distribution-1.0-SNAPSHOT-bin.zip
     0 Mon Oct 23 14:55:10 BST 2017 modules/
     0 Mon Oct 23 14:55:10 BST 2017 modules/maven-assembly-plugin/
  2086 Mon Oct 23 14:55:06 BST 2017 modules/maven-assembly-plugin/child1-1.0-SNAPSHOT.jar
  2086 Mon Oct 23 14:55:08 BST 2017 modules/maven-assembly-plugin/child2-1.0-SNAPSHOT.jar
Essex Boy
  • 7,565
  • 2
  • 21
  • 24
  • Thanks for the response. But running mvn package command is not creating an assembly. Also what should be value of output directory in binaries section? – Karthik Oct 23 '17 at 13:10
  • Also I have retained moduleSets in assembly.xml. Can assembly.xml in distribution module find other modules - child 1 and child2? – Karthik Oct 23 '17 at 13:13
  • I am getting an error after updating the distribution POM - [ERROR] Failed to execute goal on project distribution: Could not resolve dependencies for project com.karthik:distribution:pom:1.0-SNAPSHOT: The following artifacts could not be resolved: com.karthik:child1:jar:1.0-SNAPSHOT, com.ssga.fi:child2:jar:1.0-SNAPSHOT – Karthik Oct 24 '17 at 07:57
  • In my case, child 1 and child 2 packaging is WAR and not JAR. – Karthik Oct 24 '17 at 07:59
0
jar tvf distribution/target/distribution-1.0-SNAPSHOT-bin.zip

     0 Mon Oct 23 14:55:10 BST 2017 modules/
     0 Mon Oct 23 14:55:10 BST 2017 modules/maven-assembly-plugin/
  2086 Mon Oct 23 14:55:06 BST 2017 modules/maven-assembly-plugin/**child1-1.0-SNAPSHOT**.jar
  2086 Mon Oct 23 14:55:08 BST 2017 modules/maven-assembly-plugin/**child2-1.0-SNAPSHOT**.jar

Here also you can get your war and jar as you wish. Like this:

<outputFileNameMapping>${module.artifactId}.${module.extension}</outputFileNameMapping>

Use this for getting the war and jar files.

In assemble file:

<moduleSets>
<moduleSet>
  <useAllReactorProjects>true</useAllReactorProjects>
  <includes>
    <include>com.greg:child1</include>
  </includes>
  <binaries>
    <outputDirectory>modules/maven-assembly-plugin</outputDirectory>
    <unpack>false</unpack>
  </binaries>
</moduleSet>
Here just add the above line get that jar file, like this:
<moduleSets>
<moduleSet>
  <useAllReactorProjects>true</useAllReactorProjects>
  <includes>
    <include>com.greg:child1</include>
  </includes>
  <binaries>
    <outputDirectory>modules/maven-assembly-plugin</outputDirectory>
    <outputFileNameMapping>${module.artifactId}.${module.extension}</outputFileNameMapping>
    <unpack>false</unpack>
  </binaries>
</moduleSet>

Hope this will help others...

Reference: to see more

Nexonus
  • 706
  • 6
  • 26
VARUN
  • 1