Creating ZIP for Dependencies using Maven Assembly Plugin

Introduction

This post is about creating zip for dependencies using maven assembly plugin. Sometimes you may need to create a zip from your application with dependencies only, i.e., to include only the jar libraries which are being used for your application.

Then this tutorial will help you to understand how to create such zip file for jar libraries. Generally this kind of situation occurs in multi module project or for the single project also you may need to create zip file separately with jars only and such zip file is used for configuration purpose for your project or application.

Maven Assembly Plugin

Maven assembly plugin (maven-assembly-plugin) helps you to create zip file for your application.

Configuring Assembly XML

First thing is you need to configure an XML file (assembly.xml or any other name you want) under the application’s root directory or elsewhere with the following structure:

<assembly
	xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
	<id>zipLib</id>
	<formats>
		<format>zip</format>
	</formats>
	<includeBaseDirectory>false</includeBaseDirectory>
	<dependencySets>
		<dependencySet>
			<outputDirectory>lib</outputDirectory>
			<excludes>
				<exclude>${project.groupId}:${project.artifactId}:jar:*</exclude>
			</excludes>
		</dependencySet>
	</dependencySets>
</assembly>

You should put everything you want to configure inside <assembly/> tag.

You must give an id value to the <assembly/>, for example, <id>zipLib</id>, with any value you wish to give.

<includeBaseDirectory/> is by default set to true, which will result in the project’s artifactId-version and it is being used as the assembly base directory. However, in some special cases you may want to use a different directory name for the root of your assembly. That’s why I have set to false.

To include all dependencies or jar files into the zip file I have used <dependencySets/> that includes one or more <dependencySet/> tag(s).

I specified the output directory as lib, so all jar files will be put under the sub-directory lib inside the zip file.

I excluded the project’s or application’s generated jar file which is not required generally but, if you need it then you can include it. To include it simple remove <excludes/> tag.

Configuring Assembly Plugin

Now I will configure maven-assembly-plugin to generate appropriate archive.

Notice here packing type is by default jar.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<artifactId>artifact-id</artifactId>
	<parent>
		...
	</parent>
	<build>
		<plugins>
			<plugin>
				<artifactId>maven-assembly-plugin</artifactId>
				<version>2.5.2</version>
				<configuration>
					<appendAssemblyId>false</appendAssemblyId>
					<finalName>CleanupScriptsLib</finalName>
					<descriptors>
						<descriptor>${basedir}/assembly.xml</descriptor>
					</descriptors>
				</configuration>
				<executions>
					<execution>
						<id>create-archive</id>
						<phase>package</phase>
						<goals>
							<goal>single</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>

Look at the maven-assembly-plugin, I have configured few things in the above content.

I have set false to <appendAssemblyId/> to indicate that I don’t want to append the assembly id – zip – with the final zip archive.

I gave the zip archive name using tag <finalName/>. For my case, the final zip file is CleanupScriptsLib.zip.

I used <descriptor/> tag to include the assembly.xml file.

Now building the maven project or application will produce the appropriate zip file with jar files included under the lib sub-directory.

Thanks for reading.

Leave a Comment