You are viewing a plain text version of this content. The canonical link for it is here.
Posted to surefire-commits@maven.apache.org by df...@apache.org on 2007/12/09 00:53:22 UTC

svn commit: r602583 - in /maven/surefire/trunk: ./ maven-surefire-plugin/ surefire-booter/ surefire-booter/src/main/java/org/apache/maven/surefire/booter/ surefire-booter/src/test/java/org/apache/maven/surefire/booter/

Author: dfabulich
Date: Sat Dec  8 15:53:21 2007
New Revision: 602583

URL: http://svn.apache.org/viewvc?rev=602583&view=rev
Log:
Don't depend on plexus-archiver; we don't need it.  Added a simple ManifestJarWriter class instead.

Added:
    maven/surefire/trunk/surefire-booter/src/main/java/org/apache/maven/surefire/booter/ManifestJarWriter.java
    maven/surefire/trunk/surefire-booter/src/test/java/org/apache/maven/surefire/booter/ManifestJarWriterTest.java
Modified:
    maven/surefire/trunk/maven-surefire-plugin/pom.xml
    maven/surefire/trunk/pom.xml
    maven/surefire/trunk/surefire-booter/pom.xml
    maven/surefire/trunk/surefire-booter/src/main/java/org/apache/maven/surefire/booter/ForkConfiguration.java

Modified: maven/surefire/trunk/maven-surefire-plugin/pom.xml
URL: http://svn.apache.org/viewvc/maven/surefire/trunk/maven-surefire-plugin/pom.xml?rev=602583&r1=602582&r2=602583&view=diff
==============================================================================
--- maven/surefire/trunk/maven-surefire-plugin/pom.xml (original)
+++ maven/surefire/trunk/maven-surefire-plugin/pom.xml Sat Dec  8 15:53:21 2007
@@ -90,10 +90,6 @@
   </contributors>
   <dependencies>
     <dependency>
-      <groupId>org.codehaus.plexus</groupId>
-      <artifactId>plexus-archiver</artifactId>
-    </dependency>
-    <dependency>
       <groupId>org.apache.maven</groupId>
       <artifactId>maven-plugin-api</artifactId>
       <version>2.0</version>

Modified: maven/surefire/trunk/pom.xml
URL: http://svn.apache.org/viewvc/maven/surefire/trunk/pom.xml?rev=602583&r1=602582&r2=602583&view=diff
==============================================================================
--- maven/surefire/trunk/pom.xml (original)
+++ maven/surefire/trunk/pom.xml Sat Dec  8 15:53:21 2007
@@ -79,11 +79,6 @@
   <dependencyManagement>
     <dependencies>
       <dependency>
-        <groupId>org.codehaus.plexus</groupId>
-        <artifactId>plexus-archiver</artifactId>
-        <version>1.0-alpha-7</version>
-      </dependency>
-      <dependency>
         <groupId>org.apache.maven.surefire</groupId>
         <artifactId>surefire-api</artifactId>
         <version>2.4-SNAPSHOT</version>

Modified: maven/surefire/trunk/surefire-booter/pom.xml
URL: http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-booter/pom.xml?rev=602583&r1=602582&r2=602583&view=diff
==============================================================================
--- maven/surefire/trunk/surefire-booter/pom.xml (original)
+++ maven/surefire/trunk/surefire-booter/pom.xml Sat Dec  8 15:53:21 2007
@@ -9,10 +9,6 @@
   <name>SureFire Booter</name>
   <dependencies>
     <dependency>
-      <groupId>org.codehaus.plexus</groupId>
-      <artifactId>plexus-archiver</artifactId>
-    </dependency>
-    <dependency>
       <groupId>org.apache.maven.surefire</groupId>
       <artifactId>surefire-api</artifactId>
     </dependency>

Modified: maven/surefire/trunk/surefire-booter/src/main/java/org/apache/maven/surefire/booter/ForkConfiguration.java
URL: http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-booter/src/main/java/org/apache/maven/surefire/booter/ForkConfiguration.java?rev=602583&r1=602582&r2=602583&view=diff
==============================================================================
--- maven/surefire/trunk/surefire-booter/src/main/java/org/apache/maven/surefire/booter/ForkConfiguration.java (original)
+++ maven/surefire/trunk/surefire-booter/src/main/java/org/apache/maven/surefire/booter/ForkConfiguration.java Sat Dec  8 15:53:21 2007
@@ -20,10 +20,6 @@
  */
 
 import org.apache.maven.surefire.util.UrlUtils;
-import org.codehaus.plexus.archiver.ArchiverException;
-import org.codehaus.plexus.archiver.jar.JarArchiver;
-import org.codehaus.plexus.archiver.jar.Manifest;
-import org.codehaus.plexus.archiver.jar.ManifestException;
 import org.codehaus.plexus.util.StringUtils;
 import org.codehaus.plexus.util.cli.Commandline;
 
@@ -194,14 +190,6 @@
             {
                 throw new SurefireBooterForkException( "Error creating archive file", e );
             }
-            catch ( ManifestException e )
-            {
-                throw new SurefireBooterForkException( "Error creating manifest", e );
-            }
-            catch ( ArchiverException e )
-            {
-                throw new SurefireBooterForkException( "Error creating archive", e );
-            }
 
             cli.createArg().setValue( "-jar" );
 
@@ -228,20 +216,13 @@
      * @param classPath List&lt;String> of all classpath elements.
      * @return
      * @throws IOException
-     * @throws ManifestException
-     * @throws ArchiverException
      */
     private static File createJar( List classPath )
-        throws IOException, ManifestException, ArchiverException
+        throws IOException
     {
-        JarArchiver jar = new JarArchiver();
-
-        jar.setCompress( false ); // for speed
         File file = File.createTempFile( "surefirebooter", ".jar" );
         file.deleteOnExit();
-        jar.setDestFile( file );
-
-        Manifest manifest = new Manifest();
+        ManifestJarWriter writer = new ManifestJarWriter(file);
 
         // we can't use StringUtils.join here since we need to add a '/' to
         // the end of directory entries - otherwise the jvm will ignore them.
@@ -253,15 +234,10 @@
             cp += UrlUtils.getURL( new File( el ) ).toExternalForm() + " ";
         }
 
-        Manifest.Attribute attr = new Manifest.Attribute( "Class-Path", cp.trim() );
-        manifest.addConfiguredAttribute( attr );
-
-        attr = new Manifest.Attribute( "Main-Class", SurefireBooter.class.getName() );
-        manifest.addConfiguredAttribute( attr );
-
-        jar.addConfiguredManifest( manifest );
+        writer.writeValue("Class-Path", cp.trim());
+        writer.writeValue("Main-Class", SurefireBooter.class.getName());
 
-        jar.createArchive();
+        writer.close();
 
         return file;
     }

Added: maven/surefire/trunk/surefire-booter/src/main/java/org/apache/maven/surefire/booter/ManifestJarWriter.java
URL: http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-booter/src/main/java/org/apache/maven/surefire/booter/ManifestJarWriter.java?rev=602583&view=auto
==============================================================================
--- maven/surefire/trunk/surefire-booter/src/main/java/org/apache/maven/surefire/booter/ManifestJarWriter.java (added)
+++ maven/surefire/trunk/surefire-booter/src/main/java/org/apache/maven/surefire/booter/ManifestJarWriter.java Sat Dec  8 15:53:21 2007
@@ -0,0 +1,123 @@
+package org.apache.maven.surefire.booter;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+
+import java.io.*;
+import java.util.StringTokenizer;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
+
+/**
+ * Constructs a jar containing only a META-INF/MANIFEST.MF with specified attributes.
+ */
+public class ManifestJarWriter
+{
+
+    /**
+     * The max length of a line in a Manifest
+     */
+    public static final int MAX_LINE_LENGTH = 72;
+
+    /**
+     * Max length of a line section which is continued. Need to allow
+     * for the CRLF.
+     */
+    public static final int MAX_SECTION_LENGTH = MAX_LINE_LENGTH - 2;
+
+    /**
+     * The End-Of-Line marker in manifests
+     */
+    public static final String EOL = "\r\n";
+
+    private PrintWriter writer;
+
+    public ManifestJarWriter( File jarFile )
+        throws IOException
+    {
+
+        FileOutputStream fos = new FileOutputStream( jarFile );
+        ZipOutputStream zos = new ZipOutputStream( fos );
+        zos.setLevel( ZipOutputStream.STORED );
+        ZipEntry ze;
+        ze = new ZipEntry( "META-INF/MANIFEST.MF" );
+        zos.putNextEntry( ze );
+        writer = new PrintWriter( zos );
+    }
+
+    /**
+     * Write a single attribute value out.  Should handle multiple lines of attribute value.
+     *
+     * @param name  the attribute name, e.g. "Main-Class"
+     * @param value the attribute value
+     * @throws java.io.IOException if the attribute value cannot be written
+     */
+    public void writeValue( String name, String value )
+        throws IOException
+    {
+        String nameValue = name + ": " + value;
+
+        StringTokenizer tokenizer = new StringTokenizer( nameValue, "\n\r" );
+
+        String prefix = "";
+
+        while ( tokenizer.hasMoreTokens() )
+        {
+            writeLine( prefix + tokenizer.nextToken() );
+            prefix = " ";
+        }
+    }
+
+    /**
+     * Write a single Manifest line. Should handle more than 72 characters of line
+     *
+     * @param line the manifest line to be written
+     * @throws java.io.IOException if the attribute line cannot be written
+     */
+    private void writeLine( String line )
+        throws IOException
+    {
+        while ( line.getBytes().length > MAX_LINE_LENGTH )
+        {
+            // try to find a MAX_LINE_LENGTH byte section
+            int breakIndex = MAX_SECTION_LENGTH;
+            String section = line.substring( 0, breakIndex );
+            while ( section.getBytes().length > MAX_SECTION_LENGTH && breakIndex > 0 )
+            {
+                breakIndex--;
+                section = line.substring( 0, breakIndex );
+            }
+            if ( breakIndex == 0 )
+            {
+                throw new IOException( "Unable to write manifest line " + line );
+            }
+            writer.print( section + EOL );
+            line = " " + line.substring( breakIndex );
+        }
+        writer.print( line + EOL );
+    }
+
+    public void close()
+    {
+        writer.flush();
+        writer.close();
+    }
+
+}

Added: maven/surefire/trunk/surefire-booter/src/test/java/org/apache/maven/surefire/booter/ManifestJarWriterTest.java
URL: http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-booter/src/test/java/org/apache/maven/surefire/booter/ManifestJarWriterTest.java?rev=602583&view=auto
==============================================================================
--- maven/surefire/trunk/surefire-booter/src/test/java/org/apache/maven/surefire/booter/ManifestJarWriterTest.java (added)
+++ maven/surefire/trunk/surefire-booter/src/test/java/org/apache/maven/surefire/booter/ManifestJarWriterTest.java Sat Dec  8 15:53:21 2007
@@ -0,0 +1,42 @@
+package org.apache.maven.surefire.booter;
+
+import junit.framework.TestCase;
+
+import java.io.File;
+import java.io.IOException;
+
+public class ManifestJarWriterTest
+    extends TestCase
+{
+
+    File tempFile;
+
+    public void setUp()
+        throws Exception
+    {
+        tempFile = File.createTempFile( "surefirebooter", "jar" );
+        tempFile.deleteOnExit();
+    }
+
+    public void testWrite()
+        throws IOException
+    {
+        ManifestJarWriter writer = new ManifestJarWriter( tempFile );
+        writer.writeValue( "Main-Class", "Foo" );
+        writer.close();
+    }
+
+    public void tearDown()
+    {
+        tempFile.delete();
+    }
+
+    public static void main( String[] args )
+        throws Exception
+    {
+        ManifestJarWriterTest t = new ManifestJarWriterTest();
+        t.setUp();
+        t.testWrite();
+        System.out.println( t.tempFile.getAbsolutePath() );
+    }
+}