You are viewing a plain text version of this content. The canonical link for it is here.
Posted to m2-dev@maven.apache.org by ev...@apache.org on 2004/07/07 09:18:10 UTC

cvs commit: maven-components/maven-plugins/maven-jar-plugin/src/main/java/org/apache/maven/plugin/jar AbstractJarMojo.java JarMojo.java

evenisse    2004/07/07 00:18:10

  Modified:    maven-plugins/maven-jar-plugin pom.xml
               maven-plugins/maven-jar-plugin/src/main/java/org/apache/maven/plugin/jar
                        JarMojo.java
  Added:       maven-plugins/maven-jar-plugin/src/main/java/org/apache/maven/plugin/jar
                        AbstractJarMojo.java
  Log:
  Fix jar creation with code present in j2ee mojo.
  It's a temporary change before use plexus-archiver.
  
  Revision  Changes    Path
  1.5       +5 -0      maven-components/maven-plugins/maven-jar-plugin/pom.xml
  
  Index: pom.xml
  ===================================================================
  RCS file: /home/cvs/maven-components/maven-plugins/maven-jar-plugin/pom.xml,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- pom.xml	5 Jul 2004 20:10:55 -0000	1.4
  +++ pom.xml	7 Jul 2004 07:18:10 -0000	1.5
  @@ -16,6 +16,11 @@
     <dependencies>
       <dependency>
         <groupId>maven</groupId>
  +      <artifactId>maven-artifact</artifactId>
  +      <version>2.0-SNAPSHOT</version>
  +    </dependency>
  +    <dependency>
  +      <groupId>maven</groupId>
         <artifactId>maven-core</artifactId>
         <version>2.0-SNAPSHOT</version>
       </dependency>
  
  
  
  1.10      +8 -99     maven-components/maven-plugins/maven-jar-plugin/src/main/java/org/apache/maven/plugin/jar/JarMojo.java
  
  Index: JarMojo.java
  ===================================================================
  RCS file: /home/cvs/maven-components/maven-plugins/maven-jar-plugin/src/main/java/org/apache/maven/plugin/jar/JarMojo.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- JarMojo.java	6 Jul 2004 12:56:52 -0000	1.9
  +++ JarMojo.java	7 Jul 2004 07:18:10 -0000	1.10
  @@ -16,21 +16,12 @@
    * limitations under the License.
    */
   
  -import org.codehaus.plexus.util.FileUtils;
  -import org.apache.maven.plugin.AbstractPlugin;
   import org.apache.maven.plugin.PluginExecutionRequest;
   import org.apache.maven.plugin.PluginExecutionResponse;
   
  -import java.io.ByteArrayInputStream;
   import java.io.File;
  -import java.io.FileInputStream;
  -import java.io.FileOutputStream;
  -import java.io.InputStream;
  -import java.io.IOException;
  -import java.util.List;
  -import java.util.jar.JarEntry;
  -import java.util.jar.JarOutputStream;
  -import java.util.jar.Manifest;
  +import java.util.LinkedHashMap;
  +import java.util.Map;
   
   /**
    * @goal jar
  @@ -66,7 +57,7 @@
    * @version $Id$
    */
   public class JarMojo
  -    extends AbstractPlugin
  +    extends AbstractJarMojo
   {
       public void execute( PluginExecutionRequest request, PluginExecutionResponse response )
           throws Exception
  @@ -88,92 +79,10 @@
   
           File jarFile = new File( new File( outputDirectory ), jarName + ".jar" );
   
  -        List files = FileUtils.getFileNames( basedir, "**/**", "**/package.html", false );
  -
  -        createJar( files, jarFile, basedir );
  -    }
  -
  -    public void createJar( List files, File jarName, File basedir )
  -        throws Exception
  -    {
  -        JarOutputStream jar = new JarOutputStream( new FileOutputStream( jarName ), createManifest() );
  -
  -        try
  -        {
  -            for ( int i = 0; i < files.size(); i++ )
  -            {
  -                String file = (String) files.get( i );
  -
  -                writeJarEntry( jar, new File( basedir, file ), file );
  -            }
  -        }
  -        finally
  -        {
  -            jar.close();
  -        }
  -    }
  -
  -    private void writeJarEntry( JarOutputStream jar, File source, String entryName )
  -        throws Exception
  -    {
  -        byte[] buffer = new byte[1024];
  -
  -        int bytesRead;
  -
  -        try
  -        {
  -            FileInputStream is = new FileInputStream( source );
  -
  -            try
  -            {
  -                JarEntry entry = new JarEntry( entryName );
  -
  -                jar.putNextEntry( entry );
  -
  -                while ( ( bytesRead = is.read( buffer ) ) != -1 )
  -                {
  -                    jar.write( buffer, 0, bytesRead );
  -                }
  -            }
  -            catch ( Exception ex )
  -            {
  -            }
  -            finally
  -            {
  -                is.close();
  -            }
  -        }
  -        catch ( IOException ex )
  -        {
  -        }
  -    }
  -
  -    private Manifest createManifest()
  -    {
  -        Manifest manifest = null;
  +        Map includes = new LinkedHashMap();
           
  -        try
  -        {
  -            // Construct a string version of a manifest
  -            StringBuffer sbuf = new StringBuffer();
  -
  -            sbuf.append("Manifest-Version: 1.0\n");
  -
  -            sbuf.append("Created-By: Apache Maven\n");
  -
  -            sbuf.append("Built-By: " + System.getProperty("user.name") + "\n");
  -
  -            // Convert the string to a input stream
  -            InputStream is = new ByteArrayInputStream(sbuf.toString().getBytes("UTF-8"));
  -
  -            // Create the manifest
  -            manifest = new Manifest(is);
  -        }
  -        catch ( IOException e )
  -        {
  -            manifest = new Manifest();
  -        }
  -
  -        return manifest;
  +        addDirectory(includes, "**/**", "**/package.html", "", basedir);
  +        
  +        createJar( jarFile, includes );
       }
   }
  
  
  
  1.1                  maven-components/maven-plugins/maven-jar-plugin/src/main/java/org/apache/maven/plugin/jar/AbstractJarMojo.java
  
  Index: AbstractJarMojo.java
  ===================================================================
  package org.apache.maven.plugin.jar;
  
  /**
   *
   * Copyright 2004 The Apache Software Foundation
   *
   *  Licensed 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.File;
  import java.io.FileInputStream;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.util.Iterator;
  import java.util.Map;
  import java.util.Properties;
  import java.util.jar.Attributes;
  import java.util.jar.JarEntry;
  import java.util.jar.JarOutputStream;
  import java.util.jar.Manifest;
  
  import org.apache.maven.artifact.MavenArtifact;
  import org.apache.maven.plugin.AbstractPlugin;
  import org.apache.maven.project.MavenProject;
  import org.codehaus.plexus.util.DirectoryScanner;
  import org.codehaus.plexus.util.StringUtils;
  
  /**
   * Base class for tasks that build archives in JAR file format.
   *
   * @version $Revision: 1.1 $ $Date: 2004/07/07 07:18:10 $
   */
  public abstract class AbstractJarMojo
      extends AbstractPlugin
  {
      private byte[] buffer = new byte[4096];
  
      /**
       * Add artifacts from tagged dependencies to the archive.
       * @param includes a map <String, File> of items to be include in the outpur
       * @param project  the project object model
       * @param tag      the property tag to look for; for example "jar.bundle"
       * @param pathTag  the property tag that specifies the target path; for example, jar.target.path
       */
      protected void addTaggedDependencies(Map includes, MavenProject project, String tag, String pathTag) {
          addTaggedDependencies(includes, "", project, tag, pathTag);
      }
  
      /**
       * Add artifacts from tagged dependencies to the archive. For example, the definition:
       * <code>
       * <dependency>
       * <artifactId>my-library</artifactId>
       * <version>1.0.1</version>
       * <property>
       * <jar>my-library.jar
       * <property>
       * </dependency>
       * </code>
       * would result in the archive <code>my-library-1.0.1.jar</code> being included
       * in the output jar as /my-library.jar. The entry name will default to the base
       * name of the archive in the root: <code>/my-library-1.0.1.jar</code>
       *
       * @param includes a map <String, File> of items to be include in the outpur
       * @param prefix   to be added to the jar entry name of each item included
       * @param project  the project object model
       * @param tag      the property tag to look for; for example "jar.bundle"
       * @param pathTag  the property tag that specifies the target path; for example, jar.target.path
       */
      protected void addTaggedDependencies(Map includes, String prefix, MavenProject project, String tag, String pathTag) {
          for (Iterator i = project.getArtifacts().iterator(); i.hasNext();) {
              MavenArtifact artifact = (MavenArtifact) i.next();
              Properties properties = artifact.getDependency().getProperties();
              if (Boolean.valueOf(properties.getProperty(tag)).booleanValue()) {
                  File file = new File(artifact.getPath());
                  String targetPath = properties.getProperty(pathTag, file.getName());
                  includes.put(prefix + targetPath, file);
              }
          }
      }
  
      /**
       * Add all files in the specified directory to the archive.
       *
       * @param includes a map <String, File> of items to be include in the outpur
       * @param baseDir  the directory to add
       */
      protected void addDirectory(Map includes, File baseDir) throws IOException {
          addDirectory(includes, "", baseDir);
      }
  
      /**
       * Add all files in the specified directory to the archive.
       *
       * @param includes a map <String, File> of items to be include in the outpur
       * @param prefix value to be added to the front of jar entry names
       * @param baseDir  the directory to add
       */
      protected void addDirectory(Map includes, String prefix, File baseDir) throws IOException {
          addDirectory( includes, null, null, prefix, baseDir );
      }
  
      /**
       * Add all files in the specified directory to the archive.
       *
       * @param includes a map <String, File> of items to be include in the outpur
       * @param includesPattern Sets the list of include patterns to use
       * @param excludesPattern Sets the list of exclude patterns to use
       * @param prefix value to be added to the front of jar entry names
       * @param baseDir  the directory to add
       */
      protected void addDirectory(Map includes, String includesPattern, String excludesPattern, String prefix, File baseDir) throws IOException {
          DirectoryScanner scanner = new DirectoryScanner();
          scanner.setBasedir(baseDir);
          if ( includesPattern != null )
          {
              scanner.setIncludes( StringUtils.split( includesPattern, "," ) );
          }
  
          if ( excludesPattern != null )
          {
              scanner.setExcludes( StringUtils.split( excludesPattern, "," ) );
          }
          scanner.scan();
          String[] files = scanner.getIncludedFiles();
          for (int i = 0; i < files.length; i++) {
              String file = files[i];
              file = file.replace('\\', '/'); // todo shouldn't the scanner return platform independent names?
              includes.put(prefix + file, new File(baseDir, file));
          }
      }
  
      /**
       * Create the jar file specified and include the listed files.
       *
       * @param jarFile  the jar file to create
       * @param includes a Map<String, File>of items to include; the key is the jar entry name
       * @throws IOException if there is a problem writing the archive or reading the sources
       */
      protected void createJar(File jarFile, Map includes) throws IOException {
          JarOutputStream jos = createJar(jarFile, createManifest());
          try {
              addEntries(jos, includes);
          } finally {
              jos.close();
          }
      }
  
      /**
       * Create a manifest for the jar file
       *
       * @return a default manifest; the Manifest-Version and Created-By attributes are initialized
       */
      protected Manifest createManifest() {
          Manifest mf = new Manifest();
          Attributes attrs = mf.getMainAttributes();
          attrs.putValue(Attributes.Name.MANIFEST_VERSION.toString(), "1.0");
          attrs.putValue("Created-By", "2.0 (Apache Maven)");
          return mf;
      }
  
      /**
       * Create the specified jar file and return a JarOutputStream to it
       *
       * @param jarFile the jar file to create
       * @param mf      the manifest to use
       * @return a JarOutputStream that can be used to write to that file
       * @throws IOException if there was a problem opening the file
       */
      protected JarOutputStream createJar(File jarFile, Manifest mf) throws IOException {
          jarFile.getParentFile().mkdirs();
          FileOutputStream fos = new FileOutputStream(jarFile);
          try {
              return new JarOutputStream(fos, mf);
          } catch (IOException e) {
              try {
                  fos.close();
                  jarFile.delete();
              } catch (IOException e1) {
                  // ignore
              }
              throw e;
          }
      }
  
      /**
       * Add all entries in the supplied Map to the jar
       *
       * @param jos      a JarOutputStream that can be used to write to the jar
       * @param includes a Map<String, File> of entries to add
       * @throws IOException if there is a problem writing the archive or reading the sources
       */
      protected void addEntries(JarOutputStream jos, Map includes) throws IOException {
          for (Iterator i = includes.entrySet().iterator(); i.hasNext();) {
              Map.Entry entry = (Map.Entry) i.next();
              String name = (String) entry.getKey();
              File file = (File) entry.getValue();
              addEntry(jos, name, file);
          }
      }
  
      /**
       * Add a single entry to the jar
       *
       * @param jos    a JarOutputStream that can be used to write to the jar
       * @param name   the entry name to use; must be '/' delimited
       * @param source the file to add
       * @throws IOException if there is a problem writing the archive or reading the sources
       */
      protected void addEntry(JarOutputStream jos, String name, File source) throws IOException {
          FileInputStream fis = new FileInputStream(source);
          try {
              jos.putNextEntry(new JarEntry(name));
              int count;
              while ((count = fis.read(buffer)) > 0) {
                  jos.write(buffer, 0, count);
              }
              jos.closeEntry();
          } finally {
              fis.close();
          }
      }
  }