You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by br...@apache.org on 2005/10/05 08:57:00 UTC

svn commit: r295006 - in /maven/components/trunk/maven-plugins/maven-assembly-plugin: pom.xml src/main/java/org/apache/maven/plugin/assembly/AbstractUnpackingMojo.java src/main/java/org/apache/maven/plugin/assembly/AssemblyMojo.java

Author: brett
Date: Tue Oct  4 23:56:52 2005
New Revision: 295006

URL: http://svn.apache.org/viewcvs?rev=295006&view=rev
Log:
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

Modified:
    maven/components/trunk/maven-plugins/maven-assembly-plugin/pom.xml
    maven/components/trunk/maven-plugins/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/AbstractUnpackingMojo.java
    maven/components/trunk/maven-plugins/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/AssemblyMojo.java

Modified: maven/components/trunk/maven-plugins/maven-assembly-plugin/pom.xml
URL: http://svn.apache.org/viewcvs/maven/components/trunk/maven-plugins/maven-assembly-plugin/pom.xml?rev=295006&r1=295005&r2=295006&view=diff
==============================================================================
--- maven/components/trunk/maven-plugins/maven-assembly-plugin/pom.xml (original)
+++ maven/components/trunk/maven-plugins/maven-assembly-plugin/pom.xml Tue Oct  4 23:56:52 2005
@@ -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>

Modified: maven/components/trunk/maven-plugins/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/AbstractUnpackingMojo.java
URL: http://svn.apache.org/viewcvs/maven/components/trunk/maven-plugins/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/AbstractUnpackingMojo.java?rev=295006&r1=295005&r2=295006&view=diff
==============================================================================
--- maven/components/trunk/maven-plugins/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/AbstractUnpackingMojo.java (original)
+++ maven/components/trunk/maven-plugins/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/AbstractUnpackingMojo.java Tue Oct  4 23:56:52 2005
@@ -17,17 +17,16 @@
  */
 
 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 @@
      */
     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 );
         }
-    }
 
-    /**
-     * 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" ) )
+        try
         {
-            getLog().warn( "Trying to unpack a non-jar file " + file.getAbsolutePath() + " - IGNORING" );
-            return;
-        }
+            unArchiver.setSourceFile( file );
 
-        JarFile jar = new JarFile( file );
-        for ( Enumeration e = jar.entries(); e.hasMoreElements(); )
-        {
-            JarEntry entry = (JarEntry) e.nextElement();
+            unArchiver.setDestDirectory( location );
 
-            if ( !entry.isDirectory() )
-            {
-                File outFile = new File( tempLocation, entry.getName() );
-                outFile.getParentFile().mkdirs();
-                IOUtil.copy( jar.getInputStream( entry ), new FileOutputStream( outFile ) );
-            }
+            unArchiver.extract();
         }
-    }
-
-    /**
-     * 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" ) )
+        catch ( IOException ioe )
         {
-            getLog().warn( "Trying to unpack a non-zip file " + file.getAbsolutePath() + " - IGNORING" );
-            return;
+            throw new MojoExecutionException( "Error unpacking file: " + file + "to: " + location );
         }
-
-        ZipFile zip = new ZipFile( file );
-        for ( Enumeration e = zip.entries(); e.hasMoreElements(); )
+        catch ( ArchiverException e )
         {
-            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 ) );
-            }
+            throw new MojoExecutionException( "Error unpacking file: " + file + "to: " + location );
         }
     }
-
 
 }

Modified: maven/components/trunk/maven-plugins/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/AssemblyMojo.java
URL: http://svn.apache.org/viewcvs/maven/components/trunk/maven-plugins/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/AssemblyMojo.java?rev=295006&r1=295005&r2=295006&view=diff
==============================================================================
--- maven/components/trunk/maven-plugins/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/AssemblyMojo.java (original)
+++ maven/components/trunk/maven-plugins/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/AssemblyMojo.java Tue Oct  4 23:56:52 2005
@@ -32,9 +32,8 @@
 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 @@
             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 @@
      * @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 @@
                 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;
     }