PR: MNG-940

Submitted by: Dan Tran
Reviewed by:  Brett Porter
use the plexus archiver manager for unpacking and packing to simplify and expose all archivers

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@295006 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brett Leslie Porter 2005-10-05 06:56:52 +00:00
parent 1647667b16
commit 731940edd2
3 changed files with 56 additions and 97 deletions

View File

@ -52,7 +52,7 @@
<dependency>
<groupId>plexus</groupId>
<artifactId>plexus-archiver</artifactId>
<version>1.0-alpha-2</version>
<version>1.0-alpha-3-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>

View File

@ -17,17 +17,16 @@ package org.apache.maven.plugin.assembly;
*/
import org.apache.maven.plugin.AbstractMojo;
import org.codehaus.plexus.util.IOUtil;
import org.apache.maven.plugin.MojoExecutionException;
import org.codehaus.plexus.archiver.ArchiverException;
import org.codehaus.plexus.archiver.UnArchiver;
import org.codehaus.plexus.archiver.manager.ArchiverManager;
import org.codehaus.plexus.archiver.manager.NoSuchArchiverException;
import org.codehaus.plexus.util.FileUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
/**
* Base routines for assembly and unpack goals.
@ -71,88 +70,58 @@ public abstract class AbstractUnpackingMojo
*/
protected File workDirectory;
/**
* To look up Archiver/UnArchiver implementations
*
* @parameter expression="${component.org.codehaus.plexus.archiver.manager.ArchiverManager}"
* @required
*/
protected ArchiverManager archiverManager;
/**
* Unpacks the archive file.
*
* @param file File to be unpacked.
* @param location Location where to put the unpacked files.
* @throws IOException
*/
protected void unpack( File file, File location )
throws IOException
throws MojoExecutionException
{
String fileName = file.getAbsolutePath().toLowerCase().trim();
// Should be checking for '.' too?
// Not doing this to be consistent with existing code
if ( fileName.endsWith( "jar" ) )
String archiveExt = FileUtils.getExtension( file.getAbsolutePath() ).toLowerCase();
this.getLog().info( "Look up archiver type: " + archiveExt );
UnArchiver unArchiver;
try
{
unpackJar( file, location );
unArchiver = this.archiverManager.getUnArchiver( archiveExt );
}
else if ( fileName.endsWith( "zip" ) )
catch ( NoSuchArchiverException e )
{
unpackZip( file, location );
throw new MojoExecutionException( "Unknown archive file: " + file );
}
try
{
unArchiver.setSourceFile( file );
unArchiver.setDestDirectory( location );
unArchiver.extract();
}
catch ( IOException ioe )
{
throw new MojoExecutionException( "Error unpacking file: " + file + "to: " + location );
}
catch ( ArchiverException e )
{
throw new MojoExecutionException( "Error unpacking file: " + file + "to: " + location );
}
}
/**
* Unpacks the Jar file.
*
* @param file File to be unpack/unjar.
* @param tempLocation Location where to put the unpacked files.
* @throws IOException
*/
private void unpackJar( File file, File tempLocation )
throws IOException
{
if ( !file.getAbsolutePath().toLowerCase().trim().endsWith( "jar" ) )
{
getLog().warn( "Trying to unpack a non-jar file " + file.getAbsolutePath() + " - IGNORING" );
return;
}
JarFile jar = new JarFile( file );
for ( Enumeration e = jar.entries(); e.hasMoreElements(); )
{
JarEntry entry = (JarEntry) e.nextElement();
if ( !entry.isDirectory() )
{
File outFile = new File( tempLocation, entry.getName() );
outFile.getParentFile().mkdirs();
IOUtil.copy( jar.getInputStream( entry ), new FileOutputStream( outFile ) );
}
}
}
/**
* Unpacks the Zip file.
*
* @param file Zip file to be unpacked.
* @param tempLocation Location where to unpack the files.
* @throws IOException
*/
private void unpackZip( File file, File tempLocation )
throws IOException
{
if ( !file.getAbsolutePath().toLowerCase().trim().endsWith( "zip" ) )
{
getLog().warn( "Trying to unpack a non-zip file " + file.getAbsolutePath() + " - IGNORING" );
return;
}
ZipFile zip = new ZipFile( file );
for ( Enumeration e = zip.entries(); e.hasMoreElements(); )
{
ZipEntry entry = (ZipEntry) e.nextElement();
if ( !entry.isDirectory() )
{
File outFile = new File( tempLocation, entry.getName() );
outFile.getParentFile().mkdirs();
IOUtil.copy( zip.getInputStream( entry ), new FileOutputStream( outFile ) );
}
}
}
}

View File

@ -32,9 +32,8 @@ import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectHelper;
import org.codehaus.plexus.archiver.Archiver;
import org.codehaus.plexus.archiver.ArchiverException;
import org.codehaus.plexus.archiver.jar.JarArchiver;
import org.codehaus.plexus.archiver.manager.NoSuchArchiverException;
import org.codehaus.plexus.archiver.tar.TarArchiver;
import org.codehaus.plexus.archiver.zip.ZipArchiver;
import org.codehaus.plexus.util.DirectoryScanner;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.IOUtil;
@ -145,11 +144,14 @@ public class AssemblyMojo
File destFile;
try
{
// TODO: use component roles? Can we do that in a mojo?
Archiver archiver = createArchiver( format );
destFile = createArchive( archiver, assembly, filename );
}
catch ( NoSuchArchiverException e )
{
throw new MojoExecutionException( e.getMessage() );
}
catch ( ArchiverException e )
{
throw new MojoExecutionException( "Error creating assembly", e );
@ -482,13 +484,13 @@ public class AssemblyMojo
* @return archiver Archiver generated
* @throws ArchiverException
*/
private static Archiver createArchiver( String format )
throws ArchiverException
private Archiver createArchiver( String format )
throws ArchiverException, NoSuchArchiverException
{
Archiver archiver;
if ( format.startsWith( "tar" ) )
{
TarArchiver tarArchiver = new TarArchiver();
TarArchiver tarArchiver = (TarArchiver) this.archiverManager.getArchiver( "tar" );
archiver = tarArchiver;
int index = format.indexOf( '.' );
if ( index >= 0 )
@ -513,21 +515,9 @@ public class AssemblyMojo
tarArchiver.setCompression( tarCompressionMethod );
}
}
else if ( format.startsWith( "zip" ) )
{
archiver = new ZipArchiver();
}
else if ( format.startsWith( "jar" ) )
{
// TODO: use MavenArchiver for manifest?
JarArchiver jarArchiver = new JarArchiver();
jarArchiver.setCompress( true );
archiver = jarArchiver;
}
else
{
// TODO: better handling
throw new IllegalArgumentException( "Unknown format: " + format );
archiver = this.archiverManager.getArchiver( format );
}
return archiver;
}