Home > Maven > Maven – Packing and unpacking archives in Mojo with TrueZip, Part II

Maven – Packing and unpacking archives in Mojo with TrueZip, Part II

September 2, 2009 Leave a comment Go to comments

I’ve already wrote about packing and unpacking archives with TrueZip and TrueZIP Maven Plugin. Previous version to create an archive was:

 

import org.apache.maven.shared.model.fileset.FileSet;
import org.codehaus.mojo.truezip.util.DefaultTrueZip;

...

FileSet fs = new FileSet();
fs.setDirectory( sourceDirectoryPath );
fs.setIncludes( includes );
fs.setExcludes( excludes );
fs.setOutputDirectory( destinationArchivePath );

new DefaultTrueZip().copy( fs, false, getLog());
de.schlichtherle.io.File.umount(); // To update (flush) the file

 

So I was delegating the job of working with TrueZip API to the plugin. But one day someone found a case where the code above wasn’t quite working: when a single .jar file was packed into a .zip file – nothing happened and no .zip was created. Whoops ..

After debugging the code above I saw that in TrueZip-land a .jar File is a directory (I kid you not) and therefore this file was excluded from list of files to pack. And that’s why no archive was created – the list of files to pack came out empty! Which I think explained some weird behavior I’ve experienced with TrueZIP Maven Plugin in the past: when attempted to create an archive with .jar/.war files inside – nothing was created there as well and my guess is for the same reason (.jar file is a directory). Oh, well ..

Feelings aside – I now had no choice but to abandon the lovely DefaultTrueZip and talk to TrueZip directly:

for ( File file : files( sourceDirectory, includes, excludes ))
{
    String destPath = ( destinationArchive + 
file.getPath().substring( sourceDirectory.getPath().length()));
de.schlichtherle.io.File.cp_p( file,
new de.schlichtherle.io.File( destPath )); } de.schlichtherle.io.File.umount();


Here I’m packing sourceDirectory (with includes/excludes patterns) to destinationArchive.

files() simply brings us a list of files to be packed (without TrueZip voodoo):

FileSet fs = new FileSet();

fs.setDirectory( sourceDirectory );
fs.setIncludes( includes );
fs.setExcludes( excludes );

Collection<File> files = new ArrayList<File>();

for ( String filePath : new FileSetManager().getIncludedFiles( fs ))
{
    files.add( new File( sourceDirectory, filePath ));
}



destPath is important: for each file packed – destPath is file’s path inside archive.
For example: my-archive.zip/a/b/1.txt, my-archive.zip/some/other/file.jar, etc.

File’s relative path inside sourceDirectory (retrieved with substring() operation) is becoming file’s relative path inside archive.

The version above is working for all files this time, it packs all of them in a similar way and is very fast as well: it is much faster to get a list of files with files() and add them to archive one-by-one rather than call DefaultTrueZip.copy( FileSet ).

I’m planning to open-source the code above which I hope will be useful and beneficial to all Mojo authors. Also, Maven plugin that is capable to copy, (un)pack, upload, and download files (or Maven <dependencies>) is waiting to be open-sourced as well. I call it “Swiss-army knife”: it is a wild combination of assembly, resources, and dependency plugins with lot’s of sweet features like Groovy integration and scp/ftp/http support.


Stay tuned!

Advertisements
Categories: Maven Tags: , ,
  1. October 9, 2014 at 10:32

    I read a lot of interesting posts here. Probably you spend a lot of time writing, i
    know how to save you a lot of time, there is an online tool that creates unique, SEO friendly posts in seconds, just search in google – rewriter creates an unique article
    in a minute

  1. September 3, 2009 at 02:33
  2. October 25, 2010 at 06:56

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: