From 731940edd2a15304955e5de5fbc34712d65e26e7 Mon Sep 17 00:00:00 2001 From: Brett Leslie Porter Date: Wed, 5 Oct 2005 06:56:52 +0000 Subject: [PATCH] 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 --- maven-plugins/maven-assembly-plugin/pom.xml | 2 +- .../assembly/AbstractUnpackingMojo.java | 123 +++++++----------- .../maven/plugin/assembly/AssemblyMojo.java | 28 ++-- 3 files changed, 56 insertions(+), 97 deletions(-) diff --git a/maven-plugins/maven-assembly-plugin/pom.xml b/maven-plugins/maven-assembly-plugin/pom.xml index ac4a29f645..f7fa14c183 100755 --- a/maven-plugins/maven-assembly-plugin/pom.xml +++ b/maven-plugins/maven-assembly-plugin/pom.xml @@ -52,7 +52,7 @@ plexus plexus-archiver - 1.0-alpha-2 + 1.0-alpha-3-SNAPSHOT org.apache.maven diff --git a/maven-plugins/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/AbstractUnpackingMojo.java b/maven-plugins/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/AbstractUnpackingMojo.java index 0a322458a3..4c38964d4d 100644 --- a/maven-plugins/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/AbstractUnpackingMojo.java +++ b/maven-plugins/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/AbstractUnpackingMojo.java @@ -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 ) ); - } - } - } - - } diff --git a/maven-plugins/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/AssemblyMojo.java b/maven-plugins/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/AssemblyMojo.java index 24c834b9ba..f1405c52e7 100755 --- a/maven-plugins/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/AssemblyMojo.java +++ b/maven-plugins/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/AssemblyMojo.java @@ -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; }