08 - Maven assemblies

8. Maven assemblies (jar, war, ejb, ear)

            8.1 Introduction

            8.2 Build assemblies

Maven provides plugins used to create archive types that can be used as dependencies for other projects.

Customized archives are named Maven Assemblies. They can contain files, directories and dependencies that are assembled into an archive and distributed.

Because of different needs in distribution of a project sometimes is necessary to create customized archives or directories. It is possible to create a new Maven plugin that can generate custom archive format but the Assembly plugin provided by Maven saves a lot of time necessary for writing Maven code.

The Assembly plugin is the suited plugin of the process of building project distributions.

In POM it must to be added the plugin inside the plugin tag:

<project…>
<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.2.2</version>
        <configuration>
        <descriptorRefs>
            <descriptorRef>project</descriptorRef>
        </descriptorRefs>
        </plugin>
        </plugins>
    </build>
</project>

Then from CLI it has to be executed the goal:

       mvnassembly:assembly

In the target folder will be created the files with the name:<project-name>-project.zip and other two files with extension tar.

Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ myapp-web ---
[INFO]
[INFO] <<<maven-assembly-plugin:2.2.1:assembly (default-cli) < package @ myapp<<<
[INFO]
[INFO] --- maven-assembly-plugin:2.2.1:assembly (default-cli) @ myapp ---
Downloading: http://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/1.4.9/plexus-utils-1.4.9.pom
Downloaded: http://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/1.4.9/plexus-utils-1.4.9.pom (3 KB at 10.8 KB/sec)
[INFO] Building tar : C:\MyWorkspace\myapp\target\myapp-0.0.1-SNAPSHOT-project.tar.gz
[INFO] Building tar : C:\MyWorkspace\myapp\target\myapp-0.0.1-SNAPSHOT-project.tar.bz2
[INFO] Building zip: C:\MyWorkspace\myapp\target\myapp-0.0.1-SNAPSHOT-project.zip
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] myapp .............................................. SUCCESS [ 19.928 s]
[INFO] myapp-application .................................. SKIPPED
[INFO] myapp-web .......................................... SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 21.590 s
[INFO] Finished at: 2014-10-18T23:54:07+03:00
[INFO] Final Memory: 20M/49M
[INFO] ------------------------------------------------------------------------

In Windows Explorer:


 

The goal is never bound to a build lifecycle phase and should only be called as stand-alone mojo from command-line.

Mojo = Maven Old Java Object. Each mojo is an executable goal and a plugin is a distribution of one or more related mojos. This goal has the purpose to extend functionality that is not already found in Maven.

Assembly single mojo is part of every build and should be bound to a phase in the project build lifecycle.

       mvnassembly:single

There are some predefined assembly descriptors:

C:\MyWorkspace\myapp>mvn -DdescriptorId=project assembly:single
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] myapp
[INFO] myapp-application
[INFO] myapp-web
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building myapp 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-assembly-plugin:2.2.1:single (default-cli) @ myapp ---
[WARNING] The assembly id project is used more than once.
[INFO] Building tar : C:\MyWorkspace\myapp\target\myapp-0.0.1-SNAPSHOT-project.tar.gz
[INFO] Building tar : C:\MyWorkspace\myapp\target\myapp-0.0.1-SNAPSHOT-project.tar.bz2
[INFO] Building zip: C:\MyWorkspace\myapp\target\myapp-0.0.1-SNAPSHOT-project.zip
…
 [INFO] ------------------------------------------------------------------------
[INFO] Building myapp-application 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-assembly-plugin:2.2.1:single (default-cli) @ myapp-application ---
[WARNING] The assembly id project is used more than once.
[INFO] Building tar : C:\MyWorkspace\myapp\myapp-application\target\myapp-application-0.0.1-SNAPSHOT-project.
[INFO] Building tar : C:\MyWorkspace\myapp\myapp-application\target\myapp-application-0.0.1-SNAPSHOT-project.
[INFO] Building zip: C:\MyWorkspace\myapp\myapp-application\target\myapp-application-0.0.1-SNAPSHOT-project.z
……
[INFO] ------------------------------------------------------------------------
[INFO] Building myapp-web 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-assembly-plugin:2.2.1:single (default-cli) @ myapp-web ---
[WARNING] The assembly id project is used more than once.
[INFO] Building tar : C:\MyWorkspace\myapp\myapp-web\target\myapp-web-0.0.1-SNAPSHOT-project.tar.gz
[INFO] Building tar : C:\MyWorkspace\myapp\myapp-web\target\myapp-web-0.0.1-SNAPSHOT-project.tar.bz2
[INFO] Building zip: C:\MyWorkspace\myapp\myapp-web\target\myapp-web-0.0.1-SNAPSHOT-project.zip
…….
 [INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] myapp .............................................. SUCCESS [ 25.712 s]
[INFO] myapp-application .................................. SUCCESS [  3.654 s]
[INFO] myapp-web .......................................... SUCCESS [  4.288 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 34.406 s
[INFO] Finished at: 2014-10-19T00:48:24+03:00
[INFO] Final Memory: 29M/72M
[INFO] ------------------------------------------------------------------------

 

bin – is used to bundle files LICENSE,README and NOTICE with the main artifact of the project that is already built in a jar.

jar-with-dependencies – the project is built in a jar along with unpacked contents of the runtime dependencies.

project – it archives the project directory structure which when is unpacked can be built again using Maven.

src – produces an archive of the project and pom.xml along with LICENSE, README and NOTICE files.

8.2 Build assemblies

If a jar is wanted to be produced then in POM it has to be added the descriptor jar-with-dependencies.

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
        <version>2.2</version>
        <executions>
            <execution>
                <id>create-executable-jar</id>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                    </goals>
                    <configuration>
                    <descriptorRefs>
                            <descriptorRef>
                                jar-with-dependencies
                                </descriptorRef>
                            </descriptorRefs>
<archive>
                                <manifest>
                                        <mainClass>org.myapp.web.App</mainClass>
                                </manifest>
                            </archive>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

In command line it has to be executed the following command:

       mvn package

The result will be:

C:\MyWorkspace\myapp>mvn package
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] myapp
[INFO] myapp-application
[INFO] myapp-web
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building myapp 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
…
 [INFO] Building jar: C:\MyWorkspace\myapp\target\myapp-0.0.1-SNAPSHOT-jar-with-dependencies.
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building myapp-application 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
……
[INFO] Processing DependencySet (output=)
[INFO] Building jar: C:\MyWorkspace\myapp\myapp-application\target\myapp-application-0.0.1-S
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building myapp-web 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
……..
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ myapp-web ---
[INFO]
[INFO] --- maven-assembly-plugin:2.2-beta-2:single (create-executable-jar) @ myapp-web --…….
 [INFO] Building jar: C:\MyWorkspace\myapp\myapp-web\target\myapp-web-0.0.1-SNAPSHOT-jar-with
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] myapp .............................................. SUCCESS [  2.563 s]
[INFO] myapp-application .................................. SUCCESS [  4.283 s]
[INFO] myapp-web .......................................... SUCCESS [  2.645 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 9.850 s
[INFO] Finished at: 2014-10-19T01:18:23+03:00
[INFO] Final Memory: 7M/17M
[INFO] ------------------------------------------------------------------------

A new artifact is created with the classifier jar-with-dependencies.The Assembly plugin supports several archive formats:

 

- jar

- zip

- tar

-bzip2

-tar.gz

-tar.bz2

-rar

-war

-ear

-sar

-dir

If we want to get a jar with a predefined name and format we have to do the following steps:

1. Include in pom.xml in the tag plugin the following lines:

<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-1</version>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>src/main/assembly/kit.xml</descriptor>
</descriptors>
<finalName>${pom.artifactId}-${pom.version}</finalName>
<outputDirectory>build/maven/${pom.artifactId}/target</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>

2.Create the kit.xml file in src/main/assembly folder and specify the <destName>

<assembly>
<id>kit</id>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>src/main/java</directory>
</fileSet>
</fileSets>
<files>
<file>
<source>build/maven/${artifactId}/target/${artifactId}-${version}.${packaging}</source>
<outputDirectory>/</outputDirectory>
<destName>my-project.jar</destName>
</file>
</files>
</assembly>

We can replace the missing variables:

<files>
<file>
<source>target/myapp-0.0.1-SNAPSHOT.jar</source>
<outputDirectory>target</outputDirectory>
<destName>myapp.jar</destName>
</file>
</files>
The output in the console after running the command mvn package will be: 
C:\MyWorkspace\myapp>mvn package
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] myapp
[INFO] myapp-application
[INFO] myapp-web
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building myapp 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
……..
[INFO] --- maven-assembly-plugin:2.2-beta-1:single (make-assembly) @ myapp-application ---
[INFO] Reading assembly descriptor: C:\MyWorkspace\myapp\myapp-application\src\main\assembly\kit.xml
[INFO] Building zip: C:\MyWorkspace\myapp\myapp-application\target\myapp-0.0.1-kit.zip
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building myapp-web 0.0.1-SNAPSHOT
……
[INFO] Building jar: C:\MyWorkspace\myapp\myapp-web\target\myapp-web-0.0.1-SNAPSHOT-jar-with-dependencies.jar
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] myapp .............................................. SUCCESS [  2.401 s]
[INFO] myapp-application .................................. SUCCESS [  4.175 s]
[INFO] myapp-web .......................................... SUCCESS [  2.449 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 9.304 s
[INFO] Finished at: 2014-10-19T02:36:50+03:00
[INFO] Final Memory: 8M/19M
[INFO] ------------------------------------------------------------------------

Like us on Facebook