Maven assembly Plugin

From NovaOrdis Knowledge Base
Revision as of 19:10, 7 November 2016 by Ovidiu (talk | contribs) (→‎Assembly)
Jump to navigation Jump to search

External

Internal

Overview

The assembly plugin provide a standard way to aggregate complex artifacts out of project modules' artifacts, dependencies, documentation and arbitrary files. The plugin produces a single distributable archive. A module has only one artifact - usually a JAR - but we may want to distribute that JAR together with documentation, source and supporting scripts in the form or an "atomic" archive, usually created for an official release. The assembly plugin does that.

Concepts

Assembly

An assembly is an single artifact produced by aggregating artifacts produced by modules, dependencies, documentation and arbitrary files. The assembly is built according to a specification formalized in the assembly descriptor.

The Name of the Assembly Artifact

By default, the name of the assembly artifact is generated according to the pattern artifactId-version-assemblyId.extension, where:

  • "artifactId" is the value of the <artifactId> element for the project (root or sub-module) that executes the assembly plugin.
  • "version" is the value of the <version> element for the project (root or sub-module) that executes the assembly plugin.
  • "assemblyId" is the value of the <id> element for the assembly descriptor that drives the plugin. For more details on the <id> element, see [Custom_Maven_Assembly_Descriptors#Assembly_ID|Custom Maven Assembly Descriptors - the AssemblyID]].

The following configuration can be used to generate an assembly file with an arbitrary name. Properties such a ${project.version} are resolved correctly:

   <configuration>
       <finalName>example-${project.version}</finalName>
       ...
   </configuration>

For more details about the plugin configuration, see "Plugin Configuration" section, below.

Assembly Descriptor

An assembly descriptor specifies the type of assembly archive to create, the contents of the assembly, and the ways in which dependencies or its modules are bundled with an assembly. It can also contain instructions to set specific permissions, etc. Maven offers a set of predefined assembly descriptors, described below. Custom assembly descriptors can also be created.

Pre-defined Assembly Descriptors

Custom Assembly Descriptors

Custom Maven Assembly Descriptors

Plugin Configuration

<project>
    ...
    <build>
        ...
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
        </plugins>
        ...
    </build>
    ...      
</project>

Always update the plugin version to the latest stable version, to get reproducible builds.

Use <descriptorRefs> (one or more) when the pre-defined assembly descriptors are sufficient. For a list of available pre-defined assembly descriptors, see "Pre-defined Assembly Descriptors" section.

In case a custom assembly descriptor is needed, it is declared as follows:

    ...
    <configuration>
        <descriptors>
             <descriptor>src/assembly/release-artifact.xml</descriptor>
        </descriptors>
    </configuration>
    ...

For more details on custom assembly descriptors, see the "Assembly Descriptor" section.

Plugin Execution

The plugin has a single goal "single". The assembly plugin can be executed in isolation, or it can be executed during the build process of the project.

To execute the plugin in isolation:

mvn assembly:single

To execute the plugin as part of the build process, the plugin has to be first declared and configured, as described in the "Plugin Configuration" section, and then its "single" goal must be bound to the project lifecycle by associating it usually with the "package" phase, as follows:

<project>
    ...
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>...</version>
                <configuration>
                    ...
                </configuration>
                <executions>
                    <execution>
                        <id>assembly-execution</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
       ...
</project>

Once the plug-in is bound to the project lifecycle, simply execute the normal package phase from the default lifecycle to create a project assembly:

mvn package

When this build completes, the assembly file is available in the target directory and has a name similar to the following: Put placeholders: target/sample-1.0-SNAPSHOT-jar-with-dependencies.jar .

In case the assembly doesn't require binaries, so the package phase does not need to be executed, the assembly's goal can be bound to a different project phase. You should be careful to make sure the resources included in your assembly exist before that assembly is created.

The "single" Goal

More details available here: http://maven.apache.org/components/plugins/maven-assembly-plugin/single-mojo.html

Creating Two Assemblies for the Same Project

In order to create two (or more) assemblies for the same project, specify two (or more) <executions> elements.

Other Assembly Use Cases

ZIP Artifact
JAR with Dependencies
Executable JAR
Building a Maven Complex Release Artifact