Create Zip File Using Maven Assembly Plugin

Introduction

Here you will see how to create zip file using maven build tool with the help of maven-assembly-plugin. Situation may occur while one of the multi-modules project may have a module with only some configuration files, such as, shell scripts, XMLs, properties or any other files that basically do not produce a meaningful jar file, because this sub-module project does not have any Java source file.

So in such case it is better to build as a zip archive and such zip file is used for configuration purpose for your project or application.

Even if you create jar file for such module, your jar file will be empty and you will notice an warning while you are using maven tool.

Therefore it’s recommended to create a zip archive with packaging type pom, i.e., <packaging>pom</packaging>.

Maven Assembly Plugin

Maven assembly plugin helps us to create zip file for the application.

Let’s say an application or module has few configuration files in config folder under the application’s root directory.

Configuring Assembly XML

So 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>zip</id>
	<formats>
		<format>zip</format>
	</formats>
	<includeBaseDirectory>false</includeBaseDirectory>
	<fileSets>
		<fileSet>
			<directory>./config/</directory>
			<useDefaultExcludes>true</useDefaultExcludes>
			<outputDirectory>./</outputDirectory>
		</fileSet>
	</fileSets>
</assembly>

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

You must give an id value to the <assembly/>, for example, <id>zip</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.

Next I have <fileSets/> and inside it I have <fileSet/> tags. <fileSets/> basically is a collection of one or more <fileSet/> tag(s).

Using the <fileSet/> you configure which directory and files you want to include in your assembly.

In your case, you may want to include everything from the directory config under the application’s root directory using the following tag:

<directory>./config/</directory>

I am avoiding adding meta-data from the application using the following tag:

<useDefaultExcludes>true</useDefaultExcludes>

I specify the output directory using the following tag:

<outputDirectory>./</outputDirectory>

Using the above tag assembly will put all the files directly inside the zip file without creating any sub-directory.

Configuring Assembly Plugin

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

<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>
	<packaging>pom</packaging>
	<artifactId>artifact-id</artifactId>
	<parent>
		...
	</parent>
	<build>
		<plugins>
			<plugin>
				<artifactId>maven-assembly-plugin</artifactId>
				<version>2.5.2</version>
				<configuration>
					<appendAssemblyId>false</appendAssemblyId>
					<finalName>CleanupScripts</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.

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 give the zip archive name using tag <finalName/>. For my case, the final zip file is CleanupScripts.zip.

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

Including Specific Files

Let’s say, you want to include only few file types among all files, then you can use following example.

<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>zip</id>
	<formats>
		<format>zip</format>
	</formats>
	<includeBaseDirectory>false</includeBaseDirectory>
	<fileSets>
		<fileSet>
			<directory>./config/</directory>
			<useDefaultExcludes>true</useDefaultExcludes>
			<outputDirectory>./</outputDirectory>
			<includes>
				<include>*.ksh</include>
				<include>*.properties</include>
			</includes>
		</fileSet>
	</fileSets>
</assembly>

For the above example, I am assembling only files from the config directory with extensions .ksh and .properties and rest of the files will be ignored in the final zip archive.

Preserve Timestamp during Copy

Let’s say you want to also attach timestamp while you are creating zip archive file so that you can keep track of the files.

The format of the build timestamp can be customized by declaring the property maven.build.timestamp.format as shown in the example below:

<project>
  ...
  <properties>
    <maven.build.timestamp.format>yyyy-MM-dd'T'HH:mm:ss'Z'</maven.build.timestamp.format>
  </properties>
  ...
</project>

Then you can use ${maven.build.timestamp} wherever you need a timestamp property. For this example, I am going to use in the final name of the assembly file:

...
<finalName>CleanupScripts_${maven.build.timestamp}</finalName>
...

Hope you got an idea how to create an assembly file from a maven based project.

Source Code

Download

2 thoughts on “Create Zip File Using Maven Assembly Plugin

  1. Great article. It was really helpful. However, I have a question. How do you preserve the timestamp while doing the copy?

Leave a Reply

Your email address will not be published. Required fields are marked *