Coverage Report - org.apache.maven.archetype.repositorycrawler.DefaultRepositoryCrawler
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultRepositoryCrawler
66 %
37/56
70 %
14/20
9,5
 
 1  
 package org.apache.maven.archetype.repositorycrawler;
 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.commons.io.FileUtils;
 23  
 
 24  
 import org.apache.maven.archetype.catalog.Archetype;
 25  
 import org.apache.maven.archetype.catalog.ArchetypeCatalog;
 26  
 import org.apache.maven.archetype.catalog.io.xpp3.ArchetypeCatalogXpp3Writer;
 27  
 import org.apache.maven.archetype.common.ArchetypeArtifactManager;
 28  
 import org.apache.maven.archetype.exception.UnknownArchetype;
 29  
 import org.apache.maven.model.Model;
 30  
 
 31  
 import org.codehaus.plexus.logging.AbstractLogEnabled;
 32  
 import org.codehaus.plexus.util.IOUtil;
 33  
 import org.codehaus.plexus.util.StringUtils;
 34  
 import org.codehaus.plexus.util.WriterFactory;
 35  
 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
 36  
 
 37  
 import java.io.File;
 38  
 import java.io.IOException;
 39  
 import java.io.Writer;
 40  
 
 41  
 import java.util.Iterator;
 42  
 
 43  
 /**
 44  
  * @author            rafale
 45  
  * @plexus.component
 46  
  */
 47  1
 public class DefaultRepositoryCrawler
 48  
     extends AbstractLogEnabled
 49  
     implements RepositoryCrawler
 50  
 {
 51  
     /**
 52  
      * @plexus.requirement
 53  
      */
 54  
     private ArchetypeArtifactManager archetypeArtifactManager;
 55  
 
 56  
     public ArchetypeCatalog crawl( File repository )
 57  
     {
 58  1
         if ( !repository.isDirectory() )
 59  
         {
 60  0
             getLogger().warn( "File is not a directory" );
 61  0
             return null;
 62  
         }
 63  
 
 64  1
         ArchetypeCatalog catalog = new ArchetypeCatalog();
 65  
         @SuppressWarnings( "unchecked" )
 66  1
         Iterator<File> jars = FileUtils.listFiles( repository, new String[] { "jar" }, true ).iterator();
 67  
 
 68  9
         while ( jars.hasNext() )
 69  
         {
 70  8
             File jar = jars.next();
 71  8
             getLogger().info( "Scanning " + jar );
 72  8
             if ( archetypeArtifactManager.isFileSetArchetype( jar ) || archetypeArtifactManager.isOldArchetype( jar ) )
 73  
             {
 74  
                 try
 75  
                 {
 76  8
                     Archetype archetype = new Archetype();
 77  
 
 78  8
                     Model pom = archetypeArtifactManager.getArchetypePom( jar );
 79  
 
 80  8
                     if ( archetypeArtifactManager.isFileSetArchetype( jar ) )
 81  
                     {
 82  7
                         org.apache.maven.archetype.metadata.ArchetypeDescriptor descriptor =
 83  
                             archetypeArtifactManager.getFileSetArchetypeDescriptor( jar );
 84  7
                         archetype.setDescription( descriptor.getName() );
 85  7
                     }
 86  
                     else
 87  
                     {
 88  1
                         org.apache.maven.archetype.old.descriptor.ArchetypeDescriptor descriptor =
 89  
                             archetypeArtifactManager.getOldArchetypeDescriptor( jar );
 90  1
                         archetype.setDescription( descriptor.getId() );
 91  
                     }
 92  8
                     if ( pom != null )
 93  
                     {
 94  5
                         if ( pom.getGroupId() != null )
 95  
                         {
 96  5
                             archetype.setGroupId( pom.getGroupId() );
 97  
                         }
 98  
                         else
 99  
                         {
 100  0
                             archetype.setGroupId( pom.getParent().getGroupId() );
 101  
                         }
 102  5
                         archetype.setArtifactId( pom.getArtifactId() );
 103  5
                         if ( pom.getVersion() != null )
 104  
                         {
 105  5
                             archetype.setVersion( pom.getVersion() );
 106  
                         }
 107  
                         else
 108  
                         {
 109  0
                             archetype.setVersion( pom.getParent().getVersion() );
 110  
                         }
 111  5
                         getLogger().info( "\tArchetype " + archetype + " found in pom" );
 112  
                     }
 113  
                     else
 114  
                     {
 115  3
                         String version = jar.getParentFile().getName();
 116  
 
 117  3
                         String artifactId = jar.getParentFile().getParentFile().getName();
 118  
 
 119  3
                         String groupIdSep =
 120  
                             StringUtils.replace( jar.getParentFile().getParentFile().getParentFile().getPath(),
 121  
                                                  repository.getPath(), "" );
 122  3
                         String groupId = groupIdSep.replace( File.separatorChar, '.' );
 123  3
                         groupId = groupId.startsWith( "." ) ? groupId.substring( 1 ) : groupId;
 124  3
                         groupId = groupId.endsWith( "." ) ? groupId.substring( 0, groupId.length() - 1 ) : groupId;
 125  
 
 126  3
                         archetype.setGroupId( groupId );
 127  3
                         archetype.setArtifactId( artifactId );
 128  3
                         archetype.setVersion( version );
 129  
 
 130  3
                         getLogger().info( "\tArchetype " + archetype + " defined by repository path" );
 131  
                     } // end if-else
 132  
 
 133  8
                     catalog.addArchetype( archetype );
 134  
                 }
 135  0
                 catch ( XmlPullParserException ex )
 136  
                 {
 137  0
                     ex.printStackTrace();
 138  
                 }
 139  0
                 catch ( IOException ex )
 140  
                 {
 141  0
                     ex.printStackTrace();
 142  
                 }
 143  0
                 catch ( UnknownArchetype ex )
 144  
                 {
 145  0
                     ex.printStackTrace();
 146  8
                 } // end try-catch
 147  
             } // end if
 148  8
         } // end while
 149  1
         return catalog;
 150  
     }
 151  
 
 152  
     public boolean writeCatalog( ArchetypeCatalog archetypeCatalog, File archetypeCatalogFile )
 153  
     {
 154  0
         Writer fileWriter = null;
 155  
         try
 156  
         {
 157  0
             ArchetypeCatalogXpp3Writer catalogWriter = new ArchetypeCatalogXpp3Writer();
 158  
 
 159  0
             fileWriter = WriterFactory.newXmlWriter( archetypeCatalogFile );
 160  0
             catalogWriter.write( fileWriter, archetypeCatalog );
 161  0
             return true;
 162  
         }
 163  0
         catch ( IOException ex )
 164  
         {
 165  0
             getLogger().warn( "Catalog can not be writen to " + archetypeCatalogFile, ex );
 166  0
             return false;
 167  
         }
 168  
         finally
 169  
         {
 170  0
             IOUtil.close( fileWriter );
 171  
         }
 172  
     }
 173  
 }