Introduction

Here we 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 meaning jar 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 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 we 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 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 we have <fileSets/> and inside it we 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 our case, we want to include everything from the directory config under the application’s root directory using the following tag:

<directory>./config/</directory>

We are avoiding adding meta-data from the application using the following tag:

<useDefaultExcludes>true</useDefaultExcludes>

We 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 we 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, we have few things configured.

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

We give the zip archive name using tag <finalName/>. For our case, the final zip file is CleanupScripts.zip.

We 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, we are 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.

Thanks for reading.

Tags:

Leave a Reply

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