Coverage Report - org.apache.maven.archetype.DefaultArchetypeManager
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultArchetypeManager
63%
43/68
58%
7/12
2.083
 
 1  
 package org.apache.maven.archetype;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *   http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import org.apache.maven.archetype.catalog.Archetype;
 23  
 import org.apache.maven.archetype.catalog.ArchetypeCatalog;
 24  
 import org.apache.maven.archetype.creator.ArchetypeCreator;
 25  
 import org.apache.maven.archetype.generator.ArchetypeGenerator;
 26  
 import org.apache.maven.archetype.source.ArchetypeDataSource;
 27  
 import org.apache.maven.archetype.source.ArchetypeDataSourceException;
 28  
 import org.apache.maven.artifact.DependencyResolutionRequiredException;
 29  
 import org.codehaus.plexus.component.annotations.Component;
 30  
 import org.codehaus.plexus.component.annotations.Requirement;
 31  
 import org.codehaus.plexus.logging.AbstractLogEnabled;
 32  
 import org.codehaus.plexus.util.IOUtil;
 33  
 
 34  
 import java.io.File;
 35  
 import java.io.FileInputStream;
 36  
 import java.io.FileOutputStream;
 37  
 import java.io.IOException;
 38  
 import java.util.Map;
 39  
 import java.util.Properties;
 40  
 import java.util.zip.ZipEntry;
 41  
 import java.util.zip.ZipOutputStream;
 42  
 
 43  
 /**
 44  
  * @author Jason van Zyl
 45  
  */
 46  
 @Component( role = ArchetypeManager.class )
 47  12
 public class DefaultArchetypeManager
 48  
     extends AbstractLogEnabled
 49  
     implements ArchetypeManager
 50  
 {
 51  
     @Requirement( hint = "fileset" )
 52  
     private ArchetypeCreator creator;
 53  
 
 54  
     @Requirement
 55  
     private ArchetypeGenerator generator;
 56  
 
 57  
     @Requirement( role = ArchetypeDataSource.class )
 58  
     private Map<String, ArchetypeDataSource> archetypeSources;
 59  
 
 60  
     public ArchetypeCreationResult createArchetypeFromProject( ArchetypeCreationRequest request )
 61  
     {
 62  6
         ArchetypeCreationResult result = new ArchetypeCreationResult();
 63  
 
 64  6
         creator.createArchetype( request, result );
 65  
 
 66  6
         return result;
 67  
     }
 68  
 
 69  
     public ArchetypeGenerationResult generateProjectFromArchetype( ArchetypeGenerationRequest request )
 70  
     {
 71  8
         ArchetypeGenerationResult result = new ArchetypeGenerationResult();
 72  
 
 73  8
         generator.generateArchetype( request, result );
 74  
 
 75  8
         return result;
 76  
     }
 77  
 
 78  
     public File archiveArchetype( File archetypeDirectory, File outputDirectory, String finalName )
 79  
         throws DependencyResolutionRequiredException, IOException
 80  
     {
 81  6
         File jarFile = new File( outputDirectory, finalName + ".jar" );
 82  
 
 83  6
         zip( archetypeDirectory, jarFile );
 84  
 
 85  6
         return jarFile;
 86  
     }
 87  
 
 88  
     public void zip( File sourceDirectory, File archive )
 89  
         throws IOException
 90  
     {
 91  6
         if ( !archive.getParentFile().exists() )
 92  
         {
 93  0
             archive.getParentFile().mkdirs();
 94  
         }
 95  
 
 96  6
         ZipOutputStream zos = null;
 97  
         try
 98  
         {
 99  6
             zos = new ZipOutputStream( new FileOutputStream( archive ) );
 100  
 
 101  6
             zos.setLevel( 9 );
 102  
 
 103  6
             zipper( zos, sourceDirectory.getAbsolutePath().length(), sourceDirectory );
 104  
         }
 105  
         finally
 106  
         {
 107  6
             IOUtil.close( zos );
 108  6
         }
 109  
 
 110  6
     }
 111  
 
 112  
     private void zipper( ZipOutputStream zos, int offset, File currentSourceDirectory )
 113  
         throws IOException
 114  
     {
 115  120
         File[] files = currentSourceDirectory.listFiles();
 116  
 
 117  120
         if ( files.length == 0 )
 118  
         {
 119  
             // add an empty directory
 120  0
             String dirName = currentSourceDirectory.getAbsolutePath().substring( offset + 1 );
 121  
 
 122  0
             if ( File.separatorChar != '/' )
 123  
             {
 124  0
                 dirName = dirName.replace( '\\', '/' );
 125  
             }
 126  
 
 127  0
             zos.putNextEntry( new ZipEntry( dirName + '/' ) );
 128  
         }
 129  
 
 130  310
         for ( int i = 0; i < files.length; i++ )
 131  
         {
 132  190
             if ( files[i].isDirectory() )
 133  
             {
 134  114
                 zipper( zos, offset, files[i] );
 135  
             }
 136  
             else
 137  
             {
 138  76
                 String fileName = files[i].getAbsolutePath().substring( offset + 1 );
 139  
 
 140  76
                 if ( File.separatorChar != '/' )
 141  
                 {
 142  0
                     fileName = fileName.replace( '\\', '/' );
 143  
                 }
 144  
 
 145  76
                 ZipEntry e = new ZipEntry( fileName );
 146  
 
 147  76
                 zos.putNextEntry( e );
 148  
 
 149  76
                 FileInputStream is = null;
 150  
                 try
 151  
                 {
 152  76
                     is = new FileInputStream( files[i] );
 153  
 
 154  76
                     IOUtil.copy( is, zos );
 155  
                 }
 156  
                 finally
 157  
                 {
 158  76
                     IOUtil.close( is );
 159  76
                 }
 160  
 
 161  76
                 zos.flush();
 162  
 
 163  76
                 zos.closeEntry();
 164  
             }
 165  
         }
 166  120
     }
 167  
 
 168  
     public ArchetypeCatalog getInternalCatalog()
 169  
     {
 170  
         try
 171  
         {
 172  0
             ArchetypeDataSource source = archetypeSources.get( "internal-catalog" );
 173  
 
 174  0
             return source.getArchetypeCatalog( new Properties() );
 175  
         }
 176  0
         catch ( ArchetypeDataSourceException e )
 177  
         {
 178  0
             return new ArchetypeCatalog();
 179  
         }
 180  
     }
 181  
 
 182  
     public ArchetypeCatalog getDefaultLocalCatalog()
 183  
     {
 184  0
         return getLocalCatalog( "${user.home}/.m2/archetype-catalog.xml" );
 185  
     }
 186  
 
 187  
     public ArchetypeCatalog getLocalCatalog( String path )
 188  
     {
 189  
         try
 190  
         {
 191  4
             Properties properties = new Properties();
 192  4
             properties.setProperty( "file", path );
 193  4
             ArchetypeDataSource source = archetypeSources.get( "catalog" );
 194  
 
 195  4
             return source.getArchetypeCatalog( properties );
 196  
         }
 197  0
         catch ( ArchetypeDataSourceException e )
 198  
         {
 199  0
             return new ArchetypeCatalog();
 200  
         }
 201  
     }
 202  
 
 203  
     public ArchetypeCatalog getRemoteCatalog()
 204  
     {
 205  0
         return getRemoteCatalog( "http://repo1.maven.org/maven2" );
 206  
     }
 207  
 
 208  
     public ArchetypeCatalog getRemoteCatalog( String url )
 209  
     {
 210  
         try
 211  
         {
 212  2
             Properties properties = new Properties();
 213  2
             properties.setProperty( "repository", url );
 214  2
             ArchetypeDataSource source = archetypeSources.get( "remote-catalog" );
 215  
 
 216  2
             return source.getArchetypeCatalog( properties );
 217  
         }
 218  0
         catch ( ArchetypeDataSourceException e )
 219  
         {
 220  0
             return new ArchetypeCatalog();
 221  
         }
 222  
     }
 223  
 
 224  
     public void updateLocalCatalog( Archetype archetype )
 225  
     {
 226  0
         updateLocalCatalog( archetype, "${user.home}/.m2/archetype-catalog.xml" );
 227  0
     }
 228  
 
 229  
     public void updateLocalCatalog( Archetype archetype, String path )
 230  
     {
 231  
         try
 232  
         {
 233  0
             Properties properties = new Properties();
 234  0
             properties.setProperty( "file", path );
 235  0
             ArchetypeDataSource source = archetypeSources.get( "catalog" );
 236  
 
 237  0
             source.updateCatalog( properties, archetype );
 238  
         }
 239  0
         catch ( ArchetypeDataSourceException e )
 240  
         {
 241  0
         }
 242  0
     }
 243  
 }