Search code examples
mavenpackagingmaven-assembly-plugin

Maven - using assembly plugin in multi-module project to create distribution file


I'm using Maven to build a multi module project;

  • which comprises of 9 war modules

Each module has its own POM containing instructions on how to package;

<packaging>war</packaging>

A parent POM is then responsible for initiating the Maven lifecycle on each module and pushing to an artifact manager (Nexus)

<modules>
  <module>module1</module>
   <module>module2....

I would like to use the assembly plugin to package each of the WAR files which were built earlier by each module into a single ZIP file. I'm attempting to do this by defining an assembly descriptor in the parent POM which defines a separate dependencySet for each of the modules (using groupID, artifactID and version to find the WAR in my local repo).

I've tried to achieve this using the following assembly file;

<assembly>
  <id>war</id>
  <includeBaseDirectory>false</includeBaseDirectory>
  <formats>
    <format>zip</format>
  </formats>
  <dependencySets>
    <dependencySet>
      <useProjectArtifact>false</useProjectArtifact>
      <includes>
         <include>com:test-web:war:${pom.version}</include>
      </includes>
      <outputFileNameMapping>test-web.war</outputFileNameMapping>
    </dependencySet>
  </dependencySets>
</assembly>

And this is the plugin configuration in my parent POM;

<plugin>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <version>2.5.3</version>
                    <executions>
                        <execution>
                            <id>distribution-package</id>
                            <phase>package</phase>
                            <goals>
                                <goal>single</goal>
                            </goals>
                            <configuration>
                                <runOnlyAtExecutionRoot>true</runOnlyAtExecutionRoot>
                                <descriptors>
                                    <descriptor>assembly.xml</descriptor>
                                </descriptors>
                                <formats>
                                    <format>zip</format>
                                </formats>
                                <includeBaseDirectory>false</includeBaseDirectory>
                            </configuration>
                      </execution>
                    </executions>
                </plugin>

Is this approach on the right track? Additionally does it make sense to pull the WAR files from my local repo? Should i be using the output in each of the sub modules target folders instead?


Solution

  • 1) In your parent pom I would go like

    <modules>
    <module>module1</module>
    <module>distibution</module>
    <module>module2....
    

    (create an extra module for the distribution) In order to separate the parent pom from the distribution.

    2) You should not be using the output in each of the sub modules target folders. Add all wars as dependencies of the distribution. In that way maven can know that it must build the distribution LAST.

    3) Tip: replace ${pom.version} with ${project.version} (I think the former is deprecated if it works at all)