Using Component Descriptors

Introduction

Suppose you have a project which will be distributed in two forms: one for use with appserver A and another for appserver B. And as customization for these two servers, you need to exclude some dependencies which are not used by the appserver you will be distributing.

NOTE: Although putting <excludes> inside <dependencySets> may provide the result we want, it is not recommended because if a new appserver becomes available, you will have to maintain the excludes of the other distributions to exclude dependencies meant for the new appserver.

This example demonstrate the use of <componentDescriptors>, more information can be found here.

The Assembly Descriptors

First, let's write the assembly descriptor for appserver A distribution. It should like this:

  1. <assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
  4. <id>appserverA</id>
  5. <formats>
  6. <format>zip</format>
  7. </formats>
  8. <dependencySets>
  9. <dependencySet>
  10. <outputDirectory>/lib</outputDirectory>
  11. <includes>
  12. <include>application:logging</include>
  13. <include>application:core</include>
  14. <include>application:utils</include>
  15. <include>application:appserverA</include>
  16. </includes>
  17. </dependencySet>
  18. </dependencySets>
  19. </assembly>

The assembly descriptor for appserver B distribution would then be similar:

  1. <assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
  4. <id>appserverB</id>
  5. <formats>
  6. <format>zip</format>
  7. </formats>
  8. <dependencySets>
  9. <dependencySet>
  10. <outputDirectory>/lib</outputDirectory>
  11. <includes>
  12. <include>application:logging</include>
  13. <include>application:core</include>
  14. <include>application:utils</include>
  15. <include>application:appserverB</include>
  16. </includes>
  17. </dependencySet>
  18. </dependencySets>
  19. </assembly>

From the two descriptors shown, we can say that there are three artifacts common for both, thus we separate them into a common component descriptor and save it as src/assembly/component.xml. Its contents would be:

  1. <component>
  2. <dependencySets>
  3. <dependencySet>
  4. <outputDirectory>/lib</outputDirectory>
  5. <includes>
  6. <include>application:logging</include>
  7. <include>application:core</include>
  8. <include>application:utils</include>
  9. </includes>
  10. </dependencySet>
  11. </dependencySets>
  12. </component>

Then the final assembly descriptor for the appserver A would be:

  1. <assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
  4. <id>appserverA</id>
  5. <formats>
  6. <format>zip</format>
  7. </formats>
  8. <componentDescriptors>
  9. <componentDescriptor>src/assembly/component.xml</componentDescriptor>
  10. </componentDescriptors>
  11. <dependencySets>
  12. <dependencySet>
  13. <outputDirectory>/lib</outputDirectory>
  14. <includes>
  15. <include>application:appserverA</include>
  16. </includes>
  17. </dependencySet>
  18. </dependencySets>
  19. </assembly>

And the corresponding assembly descriptor for the appserver B then would be:

  1. <assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
  4. <id>appserverB</id>
  5. <formats>
  6. <format>zip</format>
  7. </formats>
  8. <componentDescriptors>
  9. <componentDescriptor>src/assembly/component.xml</componentDescriptor>
  10. </componentDescriptors>
  11. <dependencySets>
  12. <dependencySet>
  13. <outputDirectory>/lib</outputDirectory>
  14. <includes>
  15. <include>application:appserverB</include>
  16. </includes>
  17. </dependencySet>
  18. </dependencySets>
  19. </assembly>

The POM

Now we should update the POM configuration of the project for the Assembly Plugin, which should look like:

  1. <project>
  2. [...]
  3. <build>
  4. [...]
  5. <plugins>
  6. [...]
  7. <plugin>
  8. <artifactId>maven-assembly-plugin</artifactId>
  9. <version>3.0.0</version>
  10. <configuration>
  11. <descriptors>
  12. <descriptor>src/assembly/appserverA-assembly.xml</descriptor>
  13. <descriptor>src/assembly/appserverB-assembly.xml</descriptor>
  14. </descriptors>
  15. </configuration>
  16. </plugin>
  17. [...]
  18. </project>

Creating The Distributions

Since we didn't configure the assembly plugin to always generate the configured assemblies during the project's normal build lifecycle, we create the distributions by:

  1. mvn assembly:single