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="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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mattiz.maven.plugins</groupId>
  <artifactId>zipped-file-content-plugin</artifactId>
  <packaging>maven-plugin</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>zipped-file-content-plugin Maven Mojo</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>org.apache.maven</groupId>
      <artifactId>maven-plugin-api</artifactId>
      <version>2.0</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

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 MyMojo.java is created under src/main/java/com/mattiz/mavenplugins. Rename it to MattizZipDetectivePlugin.java 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.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;

/**
 * @goal explore
 *
 * @phase process-sources
 */
public class MattizZipDetectivePlugin extends AbstractMojo {
	/**
	 * Location of the file.
	 *
	 * @parameter expression="${project.build.directory}"
	 * @required
	 */
	public void execute() throws MojoExecutionException {
		try {
			listFiles(new File(
					"."+File.separator+"package_holder"+File.separator+"Mattiz.zip"));
		} catch (ZipException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	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());
				System.out.println(file.getCanonicalPath());
			}
		} finally {
			zipFile.close();
		}
	}
}

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:
zipped-file-content-plugin/target/zipped-file-content-plugin-1.0-SNAPSHOT.jar

Create a package called package_holder under root of project and place a zip file with arbitrary content called Mattiz.zip 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 Mattiz.zip.
custom-maven

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: