Maven assembly plugin

This is a note about Maven assembly plugin (version 2.3) in case someone wants to build similar distribution as I did using maven. Basically, if you try to create a custom distribution of a multimodule project and one of your modules is a web module (project of type ‘war’) with different, say, modes there are two problems:

1. How do you make the web module build result in separate artifacts for each of the modes (for instance, I had ‘optimised’ and ‘unoptimised’ modes)?

2. How do you include only one specific mode of the war artifact into the final distribution?

Answer to the first problem is: you can generate multiple war artifacts by using multiple executions of maven-war-plugin, each identified by a classifier in its configuration section. For instance:

<plugin>
        <artifactId>maven-war-plugin</artifactId>
        <executions>
                <execution>
                        <id>optimized-war</id> 
                        <goals>
                                <goal>war</goal>
                        </goals>
                        <configuration>
                                <classifier>optimised</classifier>
                                <!– configuration goes here  … –>
                        </configuration>
                </execution>
                <execution>
                        <id>optimized-war</id> 
                        <goals>
                                <goal>war</goal>
                        </goals>
                        <configuration>
                                <classifier>unoptimised</classifier>
                                <!– configuration goes here  … –>
                        </configuration>
                </execution>
        </executions>
</plugin>

An answer to the second problem could be obvious if the Assembly Plugin documentation was correct in this part:

When subelements are present, they define a set of project coordinates to include. If none is present, then represents all valid values. Artifact coordinatess may be given in simple groupId:artifactId form, or they may be fully qualified in the form groupId:artifactId:type:version[:classifier]. Additionally, wildcards can be used, as in *:maven-*

So the following won’t work (trust me, I’ve tried all possible combination!):

com.group:myartifact:war:*:optimised

Your “classified” artifact will be simply ignored and you will see a warning message in logs like this:

[WARNING] The following patterns were never triggered in this artifact exclusion filter:
o ‘com.group:myartifact:war:*:optimised’

After I have almost started pulling my hair trying to get correct include pattern string, I noticed that binaries section has optional element called attachmentClassifier. So to include only a specific artifact from a module you will want to write something like:

<moduleSet>
    <includes>
        <include>com.group:myartifact:war</include>
    </includes>
    <binaries>
        <attachmentClassifier>optimized</attachmentClassifier>
        <outputDirectory>webapps</outputDirectory>
        <unpack>false</unpack>
        <includeDependencies>false</includeDependencies>
    </binaries>
</moduleSet>

Notice also unpack parameter: by default your war file will be included unpacked.