mirror of https://github.com/apache/maven.git
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:
parent
1647667b16
commit
731940edd2
|
@ -52,7 +52,7 @@
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>plexus</groupId>
|
<groupId>plexus</groupId>
|
||||||
<artifactId>plexus-archiver</artifactId>
|
<artifactId>plexus-archiver</artifactId>
|
||||||
<version>1.0-alpha-2</version>
|
<version>1.0-alpha-3-SNAPSHOT</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.maven</groupId>
|
<groupId>org.apache.maven</groupId>
|
||||||
|
|
|
@ -17,17 +17,16 @@ package org.apache.maven.plugin.assembly;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import org.apache.maven.plugin.AbstractMojo;
|
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.File;
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Enumeration;
|
|
||||||
import java.util.Set;
|
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.
|
* Base routines for assembly and unpack goals.
|
||||||
|
@ -71,88 +70,58 @@ public abstract class AbstractUnpackingMojo
|
||||||
*/
|
*/
|
||||||
protected File workDirectory;
|
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.
|
* Unpacks the archive file.
|
||||||
*
|
*
|
||||||
* @param file File to be unpacked.
|
* @param file File to be unpacked.
|
||||||
* @param location Location where to put the unpacked files.
|
* @param location Location where to put the unpacked files.
|
||||||
* @throws IOException
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
protected void unpack( File file, File location )
|
protected void unpack( File file, File location )
|
||||||
throws IOException
|
throws MojoExecutionException
|
||||||
{
|
{
|
||||||
String fileName = file.getAbsolutePath().toLowerCase().trim();
|
String archiveExt = FileUtils.getExtension( file.getAbsolutePath() ).toLowerCase();
|
||||||
// Should be checking for '.' too?
|
|
||||||
// Not doing this to be consistent with existing code
|
this.getLog().info( "Look up archiver type: " + archiveExt );
|
||||||
if ( fileName.endsWith( "jar" ) )
|
|
||||||
|
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 ) );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,9 +32,8 @@ import org.apache.maven.project.MavenProject;
|
||||||
import org.apache.maven.project.MavenProjectHelper;
|
import org.apache.maven.project.MavenProjectHelper;
|
||||||
import org.codehaus.plexus.archiver.Archiver;
|
import org.codehaus.plexus.archiver.Archiver;
|
||||||
import org.codehaus.plexus.archiver.ArchiverException;
|
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.tar.TarArchiver;
|
||||||
import org.codehaus.plexus.archiver.zip.ZipArchiver;
|
|
||||||
import org.codehaus.plexus.util.DirectoryScanner;
|
import org.codehaus.plexus.util.DirectoryScanner;
|
||||||
import org.codehaus.plexus.util.FileUtils;
|
import org.codehaus.plexus.util.FileUtils;
|
||||||
import org.codehaus.plexus.util.IOUtil;
|
import org.codehaus.plexus.util.IOUtil;
|
||||||
|
@ -145,11 +144,14 @@ public class AssemblyMojo
|
||||||
File destFile;
|
File destFile;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// TODO: use component roles? Can we do that in a mojo?
|
|
||||||
Archiver archiver = createArchiver( format );
|
Archiver archiver = createArchiver( format );
|
||||||
|
|
||||||
destFile = createArchive( archiver, assembly, filename );
|
destFile = createArchive( archiver, assembly, filename );
|
||||||
}
|
}
|
||||||
|
catch ( NoSuchArchiverException e )
|
||||||
|
{
|
||||||
|
throw new MojoExecutionException( e.getMessage() );
|
||||||
|
}
|
||||||
catch ( ArchiverException e )
|
catch ( ArchiverException e )
|
||||||
{
|
{
|
||||||
throw new MojoExecutionException( "Error creating assembly", e );
|
throw new MojoExecutionException( "Error creating assembly", e );
|
||||||
|
@ -482,13 +484,13 @@ public class AssemblyMojo
|
||||||
* @return archiver Archiver generated
|
* @return archiver Archiver generated
|
||||||
* @throws ArchiverException
|
* @throws ArchiverException
|
||||||
*/
|
*/
|
||||||
private static Archiver createArchiver( String format )
|
private Archiver createArchiver( String format )
|
||||||
throws ArchiverException
|
throws ArchiverException, NoSuchArchiverException
|
||||||
{
|
{
|
||||||
Archiver archiver;
|
Archiver archiver;
|
||||||
if ( format.startsWith( "tar" ) )
|
if ( format.startsWith( "tar" ) )
|
||||||
{
|
{
|
||||||
TarArchiver tarArchiver = new TarArchiver();
|
TarArchiver tarArchiver = (TarArchiver) this.archiverManager.getArchiver( "tar" );
|
||||||
archiver = tarArchiver;
|
archiver = tarArchiver;
|
||||||
int index = format.indexOf( '.' );
|
int index = format.indexOf( '.' );
|
||||||
if ( index >= 0 )
|
if ( index >= 0 )
|
||||||
|
@ -513,21 +515,9 @@ public class AssemblyMojo
|
||||||
tarArchiver.setCompression( tarCompressionMethod );
|
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
|
else
|
||||||
{
|
{
|
||||||
// TODO: better handling
|
archiver = this.archiverManager.getArchiver( format );
|
||||||
throw new IllegalArgumentException( "Unknown format: " + format );
|
|
||||||
}
|
}
|
||||||
return archiver;
|
return archiver;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue