Coverage Report - org.apache.maven.archetype.ui.DefaultArchetypeGenerationConfigurator
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultArchetypeGenerationConfigurator
57%
57/100
40%
21/52
10,333
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements.  See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership.  The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License.  You may obtain a copy of the License at
 9  
  *
 10  
  *   http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied.  See the License for the
 16  
  * specific language governing permissions and limitations
 17  
  * under the License.
 18  
  */
 19  
 
 20  
 package org.apache.maven.archetype.ui;
 21  
 
 22  
 import org.apache.maven.archetype.ArchetypeGenerationRequest;
 23  
 import org.apache.maven.archetype.common.ArchetypeArtifactManager;
 24  
 import org.apache.maven.archetype.common.ArchetypeConfiguration;
 25  
 import org.apache.maven.archetype.common.ArchetypeDefinition;
 26  
 import org.apache.maven.archetype.common.ArchetypeRegistryManager;
 27  
 import org.apache.maven.archetype.common.Constants;
 28  
 import org.apache.maven.archetype.exception.ArchetypeGenerationConfigurationFailure;
 29  
 import org.apache.maven.archetype.exception.ArchetypeNotConfigured;
 30  
 import org.apache.maven.archetype.exception.ArchetypeNotDefined;
 31  
 import org.apache.maven.archetype.exception.UnknownArchetype;
 32  
 import org.apache.maven.archetype.old.OldArchetype;
 33  
 import org.apache.maven.artifact.repository.ArtifactRepository;
 34  
 
 35  
 import org.codehaus.plexus.components.interactivity.PrompterException;
 36  
 import org.codehaus.plexus.logging.AbstractLogEnabled;
 37  
 
 38  
 import java.io.IOException;
 39  
 
 40  
 import java.util.ArrayList;
 41  
 import java.util.Iterator;
 42  
 import java.util.List;
 43  
 import java.util.Properties;
 44  
 
 45  
 // TODO: this seems to have more responsibilities than just a configurator
 46  
 /**
 47  
  * @plexus.component
 48  
  */
 49  9
 public class DefaultArchetypeGenerationConfigurator
 50  
 extends AbstractLogEnabled
 51  
 implements ArchetypeGenerationConfigurator
 52  
 {
 53  
     /**
 54  
      * @plexus.requirement
 55  
      */
 56  
     OldArchetype oldArchetype;
 57  
 
 58  
     /**
 59  
      * @plexus.requirement
 60  
      */
 61  
     private ArchetypeArtifactManager archetypeArtifactManager;
 62  
 
 63  
     /**
 64  
      * @plexus.requirement
 65  
      */
 66  
     private ArchetypeFactory archetypeFactory;
 67  
 
 68  
     /**
 69  
      * @plexus.requirement
 70  
      */
 71  
     private ArchetypeGenerationQueryer archetypeGenerationQueryer;
 72  
 
 73  
     /**
 74  
      * @plexus.requirement
 75  
      */
 76  
     private ArchetypeRegistryManager archetypeRegistryManager;
 77  
 
 78  
     public void setArchetypeArtifactManager( ArchetypeArtifactManager archetypeArtifactManager )
 79  
     {
 80  9
         this.archetypeArtifactManager = archetypeArtifactManager;
 81  9
     }
 82  
 
 83  
     public void configureArchetype( ArchetypeGenerationRequest request, Boolean interactiveMode,
 84  
         Properties executionProperties )
 85  
     throws ArchetypeNotDefined,
 86  
         UnknownArchetype,
 87  
         ArchetypeNotConfigured,
 88  
         IOException,
 89  
         PrompterException,
 90  
         ArchetypeGenerationConfigurationFailure
 91  
     {
 92  9
         ArtifactRepository localRepository = request.getLocalRepository();
 93  
 
 94  9
         ArtifactRepository archetypeRepository = null;
 95  
 
 96  9
         List repositories = new ArrayList();
 97  
 
 98  9
         Properties properties = new Properties( executionProperties );
 99  
 
 100  9
         ArchetypeDefinition ad = new ArchetypeDefinition();
 101  
 
 102  9
         ad.setGroupId( request.getArchetypeGroupId() );
 103  
 
 104  9
         ad.setArtifactId( request.getArchetypeArtifactId() );
 105  
 
 106  9
         ad.setVersion( request.getArchetypeVersion() );
 107  
 
 108  9
         if( !ad.isDefined() )
 109  
         {
 110  0
             if( !interactiveMode.booleanValue() )
 111  
             {
 112  0
                 throw new ArchetypeNotDefined( "No archetype was chosen" );
 113  
             }
 114  
             else
 115  
             {
 116  0
                 throw new ArchetypeNotDefined( "The archetype is not defined" );
 117  
             }
 118  
         }
 119  9
         if( request.getArchetypeRepository() != null )
 120  
         {
 121  0
             archetypeRepository = archetypeRegistryManager.createRepository( request.getArchetypeRepository(),
 122  
                     ad.getArtifactId() + "-repo" );
 123  0
             repositories.add( archetypeRepository );
 124  
         }
 125  9
         if( request.getRemoteArtifactRepositories() != null )
 126  
         {
 127  0
             repositories.addAll( request.getRemoteArtifactRepositories() );
 128  
         }
 129  
 
 130  9
         if( !archetypeArtifactManager.exists( ad.getGroupId(), ad.getArtifactId(), ad.getVersion(), archetypeRepository,
 131  
                 localRepository, repositories ) )
 132  
         {
 133  0
             throw new UnknownArchetype( "The desired archetype does not exist (" + ad.getGroupId() + ":"
 134  
                 + ad.getArtifactId() + ":" + ad.getVersion() + ")" );
 135  
         }
 136  
 
 137  9
         request.setArchetypeVersion( ad.getVersion() );
 138  
 
 139  
         ArchetypeConfiguration archetypeConfiguration;
 140  
 
 141  9
         if( archetypeArtifactManager.isFileSetArchetype( ad.getGroupId(), ad.getArtifactId(), ad.getVersion(),
 142  
                 archetypeRepository, localRepository, repositories ) )
 143  
         {
 144  0
             org.apache.maven.archetype.metadata.ArchetypeDescriptor archetypeDescriptor = archetypeArtifactManager
 145  
                 .getFileSetArchetypeDescriptor( ad.getGroupId(), ad.getArtifactId(), ad.getVersion(),
 146  
                     archetypeRepository, localRepository, repositories );
 147  
 
 148  0
             archetypeConfiguration = archetypeFactory.createArchetypeConfiguration( archetypeDescriptor, properties );
 149  0
         }
 150  9
         else if( archetypeArtifactManager.isOldArchetype( ad.getGroupId(), ad.getArtifactId(), ad.getVersion(),
 151  
                 archetypeRepository, localRepository, repositories ) )
 152  
         {
 153  9
             org.apache.maven.archetype.old.descriptor.ArchetypeDescriptor archetypeDescriptor = archetypeArtifactManager
 154  
                 .getOldArchetypeDescriptor( ad.getGroupId(), ad.getArtifactId(), ad.getVersion(), archetypeRepository,
 155  
                     localRepository, repositories );
 156  
 
 157  9
             archetypeConfiguration = archetypeFactory.createArchetypeConfiguration( archetypeDescriptor, properties );
 158  9
         }
 159  
         else
 160  
         {
 161  0
             throw new ArchetypeGenerationConfigurationFailure( "The defined artifact is not an archetype" );
 162  
         }
 163  
 
 164  9
         if( interactiveMode.booleanValue() )
 165  
         {
 166  0
             boolean confirmed = false;
 167  
 
 168  0
             while( !confirmed )
 169  
             {
 170  0
                 if( !archetypeConfiguration.isConfigured() )
 171  
                 {
 172  0
                     Iterator requiredProperties = archetypeConfiguration.getRequiredProperties().iterator();
 173  
 
 174  0
                     while( requiredProperties.hasNext() )
 175  
                     {
 176  0
                         String requiredProperty = (String) requiredProperties.next();
 177  
 
 178  0
                         if( !archetypeConfiguration.isConfigured( requiredProperty ) )
 179  
                         {
 180  0
                             if( "package".equals(requiredProperty) ) {
 181  
                                 // if the asked property is 'package', then
 182  
                                 // use its default and if not defined,
 183  
                                 // use the 'groupId' property value.
 184  0
                                 String packageDefault = archetypeConfiguration.getDefaultValue( requiredProperty );
 185  0
                                 packageDefault = (null == packageDefault || "".equals(packageDefault))?
 186  
                                     archetypeConfiguration.getProperty( "groupId" ):
 187  
                                     archetypeConfiguration.getDefaultValue( requiredProperty );
 188  
                                 
 189  0
                                 archetypeConfiguration.setProperty( requiredProperty,
 190  
                                     archetypeGenerationQueryer.getPropertyValue( requiredProperty,
 191  
                                         packageDefault ) );
 192  0
                             } else {
 193  0
                                 archetypeConfiguration.setProperty( requiredProperty,
 194  
                                     archetypeGenerationQueryer.getPropertyValue( requiredProperty,
 195  
                                         archetypeConfiguration.getDefaultValue( requiredProperty ) ) );
 196  
                             }
 197  
                         }
 198  0
                     }
 199  
                 }
 200  
 
 201  0
                 if( !archetypeConfiguration.isConfigured() )
 202  
                 {
 203  0
                     getLogger().warn( "Archetype is not fully configured" );
 204  
                 }
 205  0
                 else if( !archetypeGenerationQueryer.confirmConfiguration( archetypeConfiguration ) )
 206  
                 {
 207  0
                     getLogger().debug( "Archetype generation configuration not confirmed" );
 208  0
                     archetypeConfiguration.reset();
 209  0
                     restoreCommandLineProperties( archetypeConfiguration, executionProperties );
 210  
                 }
 211  
                 else
 212  
                 {
 213  0
                     getLogger().debug( "Archetype generation configuration confirmed" );
 214  
 
 215  0
                     confirmed = true;
 216  
                 }
 217  
             }
 218  0
         }
 219  
         else
 220  
         {
 221  9
             if( !archetypeConfiguration.isConfigured() )
 222  
             {
 223  6
                 Iterator requiredProperties = archetypeConfiguration.getRequiredProperties().iterator();
 224  
 
 225  30
                 while( requiredProperties.hasNext() )
 226  
                 {
 227  24
                     String requiredProperty = (String) requiredProperties.next();
 228  
 
 229  24
                     if( !archetypeConfiguration.isConfigured( requiredProperty )
 230  
                         && ( archetypeConfiguration.getDefaultValue( requiredProperty ) != null ) )
 231  
                     {
 232  6
                         archetypeConfiguration.setProperty( requiredProperty,
 233  
                             archetypeConfiguration.getDefaultValue( requiredProperty ) );
 234  
                     }
 235  24
                 }
 236  
 
 237  
                 // in batch mode, we assume the defaults, and if still not configured fail
 238  6
                 if( !archetypeConfiguration.isConfigured() )
 239  
                 {
 240  3
                     StringBuffer exceptionMessage = new StringBuffer();
 241  3
                     exceptionMessage.append("Archetype " );
 242  3
                     exceptionMessage.append( request.getArchetypeGroupId() );
 243  3
                     exceptionMessage.append( ":" );
 244  3
                     exceptionMessage.append( request.getArchetypeArtifactId() );
 245  3
                     exceptionMessage.append( ":" );
 246  3
                     exceptionMessage.append( request.getArchetypeVersion() );
 247  3
                     exceptionMessage.append( " is not configured" );
 248  
                     
 249  3
                     List missingProperties = new ArrayList( 0 );
 250  3
                     requiredProperties = archetypeConfiguration.getRequiredProperties().iterator();
 251  15
                     while( requiredProperties.hasNext() )
 252  
                     {
 253  12
                         String requiredProperty = (String)requiredProperties.next();
 254  12
                         if (!archetypeConfiguration.isConfigured( requiredProperty ))
 255  
                         {
 256  6
                             exceptionMessage.append( "\n\tProperty " );
 257  6
                             exceptionMessage.append( requiredProperty );
 258  6
                             missingProperties.add( requiredProperty );
 259  6
                             exceptionMessage.append( " is missing." );
 260  6
                             getLogger().warn( "Property " + requiredProperty + 
 261  
                                 " is missing. Add -D" + requiredProperty + "=someValue" );
 262  
                         }
 263  12
                     }
 264  
                     
 265  3
                     throw new ArchetypeNotConfigured( exceptionMessage.toString(), missingProperties );
 266  
                 }
 267  
             }
 268  
         }
 269  
 
 270  6
         request.setGroupId( archetypeConfiguration.getProperty( Constants.GROUP_ID ) );
 271  
 
 272  6
         request.setArtifactId( archetypeConfiguration.getProperty( Constants.ARTIFACT_ID ) );
 273  
 
 274  6
         request.setVersion( archetypeConfiguration.getProperty( Constants.VERSION ) );
 275  
 
 276  6
         request.setPackage( archetypeConfiguration.getProperty( Constants.PACKAGE ) );
 277  
 
 278  6
         properties = archetypeConfiguration.getProperties();
 279  
 
 280  6
         request.setProperties( properties );
 281  6
     }
 282  
 
 283  
     private void restoreCommandLineProperties( ArchetypeConfiguration archetypeConfiguration,
 284  
         Properties executionProperties )
 285  
     {
 286  0
         getLogger().debug( "Restoring command line properties" );
 287  
 
 288  0
         Iterator properties = archetypeConfiguration.getRequiredProperties().iterator();
 289  0
         while( properties.hasNext() )
 290  
         {
 291  0
             String property = (String) properties.next();
 292  0
             if( executionProperties.containsKey( property ) )
 293  
             {
 294  0
                 archetypeConfiguration.setProperty( property, executionProperties.getProperty( property ) );
 295  0
                 getLogger().debug( "Restored " + property + "=" + archetypeConfiguration.getProperty( property ) );
 296  
             }
 297  0
         }
 298  0
     }
 299  
 }