Archive

Archive for September, 2009

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

September 2, 2009 3 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!

Categories: Maven Tags: , ,

Things that don’t work on Windows 2008 x64

September 1, 2009 1 comment
Categories: PC Tags: ,