Custom Maven Plugin To Investigate Contents of Zip File

Run the following command on command line at the root of your workspace folder

mvn archetype:create -DgroupId=com.mattiz.maven.plugins -DartifactId=zipped-file-content-plugin -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-mojo

It will create a default pom.xml similar to this in a folder called zipped-file-content-plugin:

<project xmlns="" xmlns:xsi=""
  <name>zipped-file-content-plugin Maven Mojo</name>

For our purpose we don’t need to add any other dependencies. This may not be the case for complex functionalities such as parsing an xml file.

Goto MattizCustomPlugin/zipped-file-content-plugin folder on command line and run mvn eclipse:eclipse. This will create an eclipse project for you. Import the eclipse project into a new workspace.

A java file called is created under src/main/java/com/mattiz/mavenplugins. Rename it to and modify the contents to look like this:

package com.mattiz.maven.plugins;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;

import java.util.Enumeration;

 * @goal explore
 * @phase process-sources
public class MattizZipDetectivePlugin extends AbstractMojo {
	 * Location of the file.
	 * @parameter expression="${}"
	 * @required
	public void execute() throws MojoExecutionException {
		try {
			listFiles(new File(
		} catch (ZipException e) {
		} catch (IOException e) {
		} catch (Exception e) {

	public void listFiles(File zippedFile) throws ZipException, IOException {
		ZipFile zipFile = new ZipFile(zippedFile);
		try {
			Enumeration<? extends ZipEntry> entries = zipFile.entries();
			while (entries.hasMoreElements()) {
				ZipEntry entry = entries.nextElement();
				File file = new File(entry.getName());
		} finally {

Note this change:

@goal explore
This defines the plugin goal.

Go to folder where the pom file lies, on command prompt and run
mvn install. This creates the snapshot jar containing your custom maven plugin here:

Create a package called package_holder under root of project and place a zip file with arbitrary content called here.

Run the following command from the command prompt.

mvn com.mattiz.maven.plugins:zipped-file-content-plugin:1.0-SNAPSHOT:explore

The command line will list the contents of

The source code can be downloaded here

About cuppajavamattiz
Matty Jacob - Avid technical blogger with interests in J2EE, Web Application Servers, Web frameworks, Open source libraries, Relational Databases, Web Services, Source control repositories, ETL, IDE Tools and related technologies.

Comments are closed.

%d bloggers like this: