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