Coverage Report - org.apache.maven.archetype.generator.DefaultArchetypeGenerator
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultArchetypeGenerator
68%
32/47
66%
12/18
3.375
 
 1  
 package org.apache.maven.archetype.generator;
 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.old.OldArchetype;
 23  
 import org.apache.maven.archetype.ArchetypeGenerationRequest;
 24  
 import org.apache.maven.archetype.ArchetypeGenerationResult;
 25  
 import org.apache.maven.archetype.common.ArchetypeArtifactManager;
 26  
 import org.apache.maven.archetype.common.ArchetypeRegistryManager;
 27  
 import org.apache.maven.archetype.exception.ArchetypeException;
 28  
 import org.apache.maven.archetype.exception.ArchetypeGenerationFailure;
 29  
 import org.apache.maven.archetype.exception.ArchetypeNotDefined;
 30  
 import org.apache.maven.archetype.exception.UnknownArchetype;
 31  
 import org.apache.maven.artifact.repository.ArtifactRepository;
 32  
 import org.codehaus.plexus.component.annotations.Component;
 33  
 import org.codehaus.plexus.component.annotations.Requirement;
 34  
 import org.codehaus.plexus.logging.AbstractLogEnabled;
 35  
 import org.codehaus.plexus.util.StringUtils;
 36  
 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
 37  
 import org.dom4j.DocumentException;
 38  
 
 39  
 import java.io.File;
 40  
 import java.io.IOException;
 41  
 import java.util.ArrayList;
 42  
 import java.util.List;
 43  
 
 44  
 @Component( role = ArchetypeGenerator.class )
 45  34
 public class DefaultArchetypeGenerator
 46  
     extends AbstractLogEnabled
 47  
     implements ArchetypeGenerator
 48  
 {
 49  
     @Requirement
 50  
     private ArchetypeRegistryManager archetypeRegistryManager;
 51  
 
 52  
     @Requirement
 53  
     private ArchetypeArtifactManager archetypeArtifactManager;
 54  
 
 55  
     @Requirement
 56  
     private FilesetArchetypeGenerator filesetGenerator;
 57  
 
 58  
     @Requirement
 59  
     private OldArchetype oldArchetype;
 60  
 
 61  
     private File getArchetypeFile( ArchetypeGenerationRequest request, ArtifactRepository localRepository )
 62  
         throws IOException, ArchetypeException, XmlPullParserException, DocumentException
 63  
     {
 64  30
         if ( !isArchetypeDefined( request ) )
 65  
         {
 66  2
             throw new ArchetypeNotDefined( "The archetype is not defined" );
 67  
         }
 68  
 
 69  28
         List<ArtifactRepository> repos = new ArrayList<ArtifactRepository>();
 70  
 
 71  28
         ArtifactRepository remoteRepo = null;
 72  28
         if ( request != null && request.getArchetypeRepository() != null )
 73  
         {
 74  28
             remoteRepo =
 75  
                 archetypeRegistryManager.createRepository( request.getArchetypeRepository(),
 76  
                                                            request.getArchetypeArtifactId() + "-repo" );
 77  
 
 78  28
             repos.add( remoteRepo );
 79  
         }
 80  
 
 81  28
         if ( !archetypeArtifactManager.exists( request.getArchetypeGroupId(), request.getArchetypeArtifactId(),
 82  
                                                request.getArchetypeVersion(), remoteRepo, localRepository, repos ) )
 83  
         {
 84  0
             throw new UnknownArchetype( "The desired archetype does not exist (" + request.getArchetypeGroupId() + ":"
 85  
                 + request.getArchetypeArtifactId() + ":" + request.getArchetypeVersion() + ")" );
 86  
         }
 87  
 
 88  28
         File archetypeFile =
 89  
             archetypeArtifactManager.getArchetypeFile( request.getArchetypeGroupId(), request.getArchetypeArtifactId(),
 90  
                                                        request.getArchetypeVersion(), remoteRepo, localRepository,
 91  
                                                        repos );
 92  28
         return archetypeFile;
 93  
     }
 94  
 
 95  
     private void generateArchetype( ArchetypeGenerationRequest request, File archetypeFile )
 96  
         throws IOException, ArchetypeException, XmlPullParserException, DocumentException
 97  
     {
 98  28
         if ( archetypeArtifactManager.isFileSetArchetype( archetypeFile ) )
 99  
         {
 100  26
             processFileSetArchetype( request, archetypeFile );
 101  
         }
 102  2
         else if ( archetypeArtifactManager.isOldArchetype( archetypeFile ) )
 103  
         {
 104  2
             processOldArchetype( request, archetypeFile );
 105  
         }
 106  
         else
 107  
         {
 108  0
             throw new ArchetypeGenerationFailure( "The defined artifact is not an archetype" );
 109  
         }
 110  26
     }
 111  
 
 112  
     /** Common */
 113  
     public String getPackageAsDirectory( String packageName )
 114  
     {
 115  0
         return StringUtils.replace( packageName, ".", "/" );
 116  
     }
 117  
 
 118  
     private boolean isArchetypeDefined( ArchetypeGenerationRequest request )
 119  
     {
 120  30
         return StringUtils.isNotEmpty( request.getArchetypeGroupId() )
 121  
             && StringUtils.isNotEmpty( request.getArchetypeArtifactId() )
 122  
             && StringUtils.isNotEmpty( request.getArchetypeVersion() );
 123  
     }
 124  
 
 125  
     /** FileSetArchetype */
 126  
     private void processFileSetArchetype( ArchetypeGenerationRequest request, File archetypeFile )
 127  
         throws ArchetypeException
 128  
     {
 129  26
         filesetGenerator.generateArchetype( request, archetypeFile );
 130  24
     }
 131  
 
 132  
     private void processOldArchetype( ArchetypeGenerationRequest request, File archetypeFile )
 133  
         throws UnknownArchetype, ArchetypeGenerationFailure
 134  
     {
 135  2
         oldArchetype.createArchetype( request, archetypeFile );
 136  2
     }
 137  
 
 138  
     public void generateArchetype( ArchetypeGenerationRequest request, File archetypeFile,
 139  
                                    ArchetypeGenerationResult result )
 140  
     {
 141  
         try
 142  
         {
 143  28
             generateArchetype( request, archetypeFile );
 144  
         }
 145  0
         catch ( IOException ex )
 146  
         {
 147  0
             result.setCause( ex );
 148  
         }
 149  2
         catch ( ArchetypeException ex )
 150  
         {
 151  2
             result.setCause( ex );
 152  
         }
 153  0
         catch ( XmlPullParserException ex )
 154  
         {
 155  0
             result.setCause( ex );
 156  
         }
 157  0
         catch ( DocumentException ex )
 158  
         {
 159  0
             result.setCause( ex );
 160  28
         }
 161  28
     }
 162  
 
 163  
     public void generateArchetype( ArchetypeGenerationRequest request, ArchetypeGenerationResult result )
 164  
     {
 165  
         try
 166  
         {
 167  30
             File archetypeFile = getArchetypeFile( request, request.getLocalRepository() );
 168  
 
 169  28
             generateArchetype( request, archetypeFile, result );
 170  
         }
 171  0
         catch ( IOException ex )
 172  
         {
 173  0
             result.setCause( ex );
 174  
         }
 175  2
         catch ( ArchetypeException ex )
 176  
         {
 177  2
             result.setCause( ex );
 178  
         }
 179  0
         catch ( XmlPullParserException ex )
 180  
         {
 181  0
             result.setCause( ex );
 182  
         }
 183  0
         catch ( DocumentException ex )
 184  
         {
 185  0
             result.setCause( ex );
 186  30
         }
 187  30
     }
 188  
 }