Coverage Report - org.apache.maven.archetype.ui.creation.DefaultArchetypeCreationConfigurator
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultArchetypeCreationConfigurator
0 %
0/136
0 %
0/72
6,571
 
 1  
 package org.apache.maven.archetype.ui.creation;
 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.common.ArchetypeFilesResolver;
 23  
 import org.apache.maven.archetype.common.Constants;
 24  
 import org.apache.maven.archetype.exception.ArchetypeNotConfigured;
 25  
 import org.apache.maven.archetype.exception.ArchetypeNotDefined;
 26  
 import org.apache.maven.archetype.exception.TemplateCreationException;
 27  
 import org.apache.maven.archetype.ui.ArchetypeConfiguration;
 28  
 import org.apache.maven.archetype.ui.ArchetypeDefinition;
 29  
 import org.apache.maven.archetype.ui.ArchetypeFactory;
 30  
 import org.apache.maven.project.MavenProject;
 31  
 import org.codehaus.plexus.components.interactivity.PrompterException;
 32  
 import org.codehaus.plexus.logging.AbstractLogEnabled;
 33  
 import org.codehaus.plexus.util.IOUtil;
 34  
 import org.codehaus.plexus.util.StringUtils;
 35  
 
 36  
 import java.io.File;
 37  
 import java.io.FileInputStream;
 38  
 import java.io.FileNotFoundException;
 39  
 import java.io.FileOutputStream;
 40  
 import java.io.IOException;
 41  
 import java.io.InputStream;
 42  
 import java.io.OutputStream;
 43  
 import java.util.ArrayList;
 44  
 import java.util.Iterator;
 45  
 import java.util.List;
 46  
 import java.util.Properties;
 47  
 
 48  
 /** @plexus.component */
 49  0
 public class DefaultArchetypeCreationConfigurator
 50  
     extends AbstractLogEnabled
 51  
     implements ArchetypeCreationConfigurator
 52  
 {
 53  
     /** @plexus.requirement */
 54  
     private ArchetypeCreationQueryer archetypeCreationQueryer;
 55  
 
 56  
     /** @plexus.requirement */
 57  
     private ArchetypeFactory archetypeFactory;
 58  
 
 59  
     /** @plexus.requirement */
 60  
     private ArchetypeFilesResolver archetypeFilesResolver;
 61  
 
 62  
     public Properties configureArchetypeCreation( MavenProject project, Boolean interactiveMode,
 63  
                                                   Properties commandLineProperties, File propertyFile,
 64  
                                                   List<String> languages )
 65  
         throws IOException, ArchetypeNotDefined, ArchetypeNotConfigured, PrompterException, TemplateCreationException
 66  
     {
 67  0
         Properties properties = initialiseArchetypeProperties( commandLineProperties, propertyFile );
 68  
 
 69  0
         ArchetypeDefinition archetypeDefinition = archetypeFactory.createArchetypeDefinition( properties );
 70  
 
 71  0
         if ( !archetypeDefinition.isDefined() )
 72  
         {
 73  0
             archetypeDefinition = defineDefaultArchetype( project, properties );
 74  
         }
 75  
 
 76  0
         ArchetypeConfiguration archetypeConfiguration =
 77  
             archetypeFactory.createArchetypeConfiguration( project, archetypeDefinition, properties );
 78  
 
 79  0
         String resolvedPackage = archetypeFilesResolver.resolvePackage( project.getBasedir(), languages );
 80  
 
 81  0
         if ( !archetypeConfiguration.isConfigured() )
 82  
         {
 83  0
             archetypeConfiguration =
 84  
                 defineDefaultConfiguration( project, archetypeDefinition, resolvedPackage, properties );
 85  
         }
 86  
 
 87  0
         if ( interactiveMode.booleanValue() )
 88  
         {
 89  0
             getLogger().debug( "Entering interactive mode" );
 90  
 
 91  0
             boolean confirmed = false;
 92  0
             while ( !confirmed )
 93  
             {
 94  0
                 if ( !archetypeDefinition.isDefined() )// <editor-fold text="...">
 95  
                 {
 96  0
                     getLogger().debug( "Archetype is yet not defined" );
 97  0
                     if ( !archetypeDefinition.isGroupDefined() )
 98  
                     {
 99  0
                         getLogger().debug( "Asking for archetype's groupId" );
 100  0
                         archetypeDefinition.setGroupId( archetypeCreationQueryer.getArchetypeGroupId( project.getGroupId() ) );
 101  
                     }
 102  0
                     if ( !archetypeDefinition.isArtifactDefined() )
 103  
                     {
 104  0
                         getLogger().debug( "Asking for archetype's artifactId" );
 105  0
                         archetypeDefinition.setArtifactId( archetypeCreationQueryer.getArchetypeArtifactId( project.getArtifactId()
 106  
                             + Constants.ARCHETYPE_SUFFIX ) );
 107  
                     }
 108  0
                     if ( !archetypeDefinition.isVersionDefined() )
 109  
                     {
 110  0
                         getLogger().debug( "Asking for archetype's version" );
 111  0
                         archetypeDefinition.setVersion( archetypeCreationQueryer.getArchetypeVersion( project.getVersion() ) );
 112  
                     }
 113  
 
 114  0
                     archetypeFactory.updateArchetypeConfiguration( archetypeConfiguration, archetypeDefinition );
 115  
                 } // </editor-fold>
 116  
 
 117  0
                 if ( !archetypeConfiguration.isConfigured() )// <editor-fold text="...">
 118  
                 {
 119  0
                     getLogger().debug( "Archetype is not yet configured" );
 120  0
                     if ( !archetypeConfiguration.isConfigured( Constants.GROUP_ID ) )
 121  
                     {
 122  0
                         getLogger().debug( "Asking for project's groupId" );
 123  0
                         archetypeConfiguration.setProperty(
 124  
                                                             Constants.GROUP_ID,
 125  
                                                             archetypeCreationQueryer.getGroupId( archetypeConfiguration.getDefaultValue( Constants.GROUP_ID ) ) );
 126  
                     }
 127  0
                     if ( !archetypeConfiguration.isConfigured( Constants.ARTIFACT_ID ) )
 128  
                     {
 129  0
                         getLogger().debug( "Asking for project's artifactId" );
 130  0
                         archetypeConfiguration.setProperty(
 131  
                                                             Constants.ARTIFACT_ID,
 132  
                                                             archetypeCreationQueryer.getArtifactId( archetypeConfiguration.getDefaultValue( Constants.ARTIFACT_ID ) ) );
 133  
                     }
 134  0
                     if ( !archetypeConfiguration.isConfigured( Constants.VERSION ) )
 135  
                     {
 136  0
                         getLogger().debug( "Asking for project's version" );
 137  0
                         archetypeConfiguration.setProperty(
 138  
                                                             Constants.VERSION,
 139  
                                                             archetypeCreationQueryer.getVersion( archetypeConfiguration.getDefaultValue( Constants.VERSION ) ) );
 140  
                     }
 141  0
                     if ( !archetypeConfiguration.isConfigured( Constants.PACKAGE ) )
 142  
                     {
 143  0
                         getLogger().debug( "Asking for project's package" );
 144  0
                         archetypeConfiguration.setProperty(
 145  
                                                             Constants.PACKAGE,
 146  
                                                             archetypeCreationQueryer.getPackage( StringUtils.isEmpty( resolvedPackage ) ? archetypeConfiguration.getDefaultValue( Constants.PACKAGE )
 147  
                                                                             : resolvedPackage ) );
 148  
                     }
 149  
                 } // </editor-fold>
 150  
 
 151  0
                 boolean stopAddingProperties = false;
 152  0
                 while ( !stopAddingProperties )
 153  
                 {
 154  0
                     getLogger().debug( "Asking for another required property" );
 155  0
                     stopAddingProperties = !archetypeCreationQueryer.askAddAnotherProperty();
 156  
 
 157  0
                     if ( !stopAddingProperties )
 158  
                     {
 159  0
                         getLogger().debug( "Asking for required property key" );
 160  
 
 161  0
                         String propertyKey = archetypeCreationQueryer.askNewPropertyKey();
 162  0
                         getLogger().debug( "Asking for required property value" );
 163  
 
 164  0
                         String replacementValue =
 165  
                             archetypeCreationQueryer.askReplacementValue(
 166  
                                                                           propertyKey,
 167  
                                                                           archetypeConfiguration.getDefaultValue( propertyKey ) );
 168  0
                         archetypeConfiguration.setDefaultProperty( propertyKey, replacementValue );
 169  0
                         archetypeConfiguration.setProperty( propertyKey, replacementValue );
 170  0
                     }
 171  
                 }
 172  
 
 173  0
                 getLogger().debug( "Asking for configuration confirmation" );
 174  0
                 if ( archetypeCreationQueryer.confirmConfiguration( archetypeConfiguration ) )
 175  
                 {
 176  0
                     confirmed = true;
 177  
                 }
 178  
                 else
 179  
                 {
 180  0
                     getLogger().debug( "Reseting archetype's definition and configuration" );
 181  0
                     archetypeConfiguration.reset();
 182  0
                     archetypeDefinition.reset();
 183  
                 }
 184  0
             } // end while
 185  0
         }
 186  
         else
 187  
         {
 188  0
             getLogger().debug( "Entering batch mode" );
 189  0
             if ( !archetypeDefinition.isDefined() )
 190  
             {
 191  0
                 throw new ArchetypeNotDefined( "The archetype is not defined" );
 192  
             }
 193  0
             else if ( !archetypeConfiguration.isConfigured() )
 194  
             {
 195  0
                 throw new ArchetypeNotConfigured( "The archetype is not configured", null );
 196  
             }
 197  
         } // end if
 198  
 
 199  0
         return archetypeConfiguration.toProperties();
 200  
     }
 201  
 
 202  
     private ArchetypeDefinition defineDefaultArchetype( MavenProject project, Properties properties )
 203  
     {
 204  0
         if ( StringUtils.isEmpty( properties.getProperty( Constants.ARCHETYPE_GROUP_ID ) ) )
 205  
         {
 206  0
             properties.setProperty( Constants.ARCHETYPE_GROUP_ID, project.getGroupId() );
 207  
         }
 208  0
         if ( StringUtils.isEmpty( properties.getProperty( Constants.ARCHETYPE_ARTIFACT_ID ) ) )
 209  
         {
 210  0
             properties.setProperty( Constants.ARCHETYPE_ARTIFACT_ID, project.getArtifactId()
 211  
                 + Constants.ARCHETYPE_SUFFIX );
 212  
         }
 213  0
         if ( StringUtils.isEmpty( properties.getProperty( Constants.ARCHETYPE_VERSION ) ) )
 214  
         {
 215  0
             properties.setProperty( Constants.ARCHETYPE_VERSION, project.getVersion() );
 216  
         }
 217  
 
 218  0
         return archetypeFactory.createArchetypeDefinition( properties );
 219  
     }
 220  
 
 221  
     private ArchetypeConfiguration defineDefaultConfiguration( MavenProject project,
 222  
                                                                ArchetypeDefinition archetypeDefinition,
 223  
                                                                String resolvedPackage, Properties properties )
 224  
     {
 225  0
         if ( StringUtils.isEmpty( properties.getProperty( Constants.GROUP_ID ) ) )
 226  
         {
 227  0
             getLogger().info( "Setting default groupId: " + project.getGroupId() );
 228  0
             properties.setProperty( Constants.GROUP_ID, project.getGroupId() );
 229  
         }
 230  
 
 231  0
         if ( StringUtils.isEmpty( properties.getProperty( Constants.ARTIFACT_ID ) ) )
 232  
         {
 233  0
             getLogger().info( "Setting default artifactId: " + project.getArtifactId() );
 234  0
             properties.setProperty( Constants.ARTIFACT_ID, project.getArtifactId() );
 235  
         }
 236  
 
 237  0
         if ( StringUtils.isEmpty( properties.getProperty( Constants.VERSION ) ) )
 238  
         {
 239  0
             getLogger().info( "Setting default version: " + project.getVersion() );
 240  0
             properties.setProperty( Constants.VERSION, project.getVersion() );
 241  
         }
 242  
 
 243  0
         if ( StringUtils.isEmpty( properties.getProperty( Constants.ARCHETYPE_GROUP_ID ) ) )
 244  
         {
 245  0
             getLogger().info( "Setting default archetype's groupId: " + project.getGroupId() );
 246  0
             properties.setProperty( Constants.ARCHETYPE_GROUP_ID, project.getGroupId() );
 247  
         }
 248  
 
 249  0
         if ( StringUtils.isEmpty( properties.getProperty( Constants.ARCHETYPE_ARTIFACT_ID ) ) )
 250  
         {
 251  0
             getLogger().info( "Setting default archetype's artifactId: " + project.getArtifactId() );
 252  0
             properties.setProperty( Constants.ARCHETYPE_ARTIFACT_ID, project.getArtifactId()
 253  
                 + Constants.ARCHETYPE_SUFFIX );
 254  
         }
 255  
 
 256  0
         if ( StringUtils.isEmpty( properties.getProperty( Constants.ARCHETYPE_VERSION ) ) )
 257  
         {
 258  0
             getLogger().info( "Setting default archetype's version: " + project.getVersion() );
 259  0
             properties.setProperty( Constants.ARCHETYPE_VERSION, project.getVersion() );
 260  
         }
 261  
 
 262  0
         if ( StringUtils.isEmpty( properties.getProperty( Constants.PACKAGE /*, properties.getProperty ( Constants.PACKAGE_NAME ) */ ) ) )
 263  
         {
 264  0
             if ( StringUtils.isEmpty( resolvedPackage ) )
 265  
             {
 266  0
                 resolvedPackage = project.getGroupId();
 267  
             }
 268  0
             getLogger().info( "Setting default package: " + resolvedPackage );
 269  
             /* properties.setProperty ( Constants.PACKAGE_NAME, resolvedPackage ); */
 270  0
             properties.setProperty( Constants.PACKAGE, resolvedPackage );
 271  
         }
 272  
 
 273  0
         return archetypeFactory.createArchetypeConfiguration( project, archetypeDefinition, properties );
 274  
     }
 275  
 
 276  
     public void readProperties( Properties properties, File propertyFile )
 277  
         throws IOException
 278  
     {
 279  0
         getLogger().debug( "Reading property file " + propertyFile );
 280  
 
 281  0
         InputStream is = new FileInputStream( propertyFile );
 282  
 
 283  
         try
 284  
         {
 285  0
             properties.load( is );
 286  
 
 287  0
             getLogger().debug( "Read " + properties.size() + " properties" );
 288  
         }
 289  
         finally
 290  
         {
 291  0
             IOUtil.close( is );
 292  0
         }
 293  0
     }
 294  
 
 295  
     public void writeProperties( Properties properties, File propertyFile )
 296  
         throws IOException
 297  
     {
 298  0
         Properties storedProperties = new Properties();
 299  
         try
 300  
         {
 301  0
             readProperties( storedProperties, propertyFile );
 302  
         }
 303  0
         catch ( FileNotFoundException ex )
 304  
         {
 305  0
             getLogger().debug( "Property file not found. Creating a new one" );
 306  0
         }
 307  
 
 308  0
         getLogger().debug( "Adding " + properties.size() + " properties" );
 309  
 
 310  0
         Iterator<?> propertiesIterator = properties.keySet().iterator();
 311  0
         while ( propertiesIterator.hasNext() )
 312  
         {
 313  0
             String propertyKey = (String) propertiesIterator.next();
 314  0
             storedProperties.setProperty( propertyKey, properties.getProperty( propertyKey ) );
 315  0
         }
 316  
 
 317  0
         OutputStream os = new FileOutputStream( propertyFile );
 318  
 
 319  
         try
 320  
         {
 321  0
             storedProperties.store( os, "" );
 322  
 
 323  0
             getLogger().debug( "Stored " + storedProperties.size() + " properties" );
 324  
         }
 325  
         finally
 326  
         {
 327  0
             IOUtil.close( os );
 328  0
         }
 329  0
     }
 330  
 
 331  
     private Properties initialiseArchetypeProperties( Properties commandLineProperties, File propertyFile )
 332  
         throws IOException
 333  
     {
 334  0
         Properties properties = new Properties();
 335  
 
 336  0
         if ( propertyFile != null )
 337  
         {
 338  
             try
 339  
             {
 340  0
                 readProperties( properties, propertyFile );
 341  
             }
 342  0
             catch ( FileNotFoundException ex )
 343  
             {
 344  0
                 getLogger().debug( "archetype.properties does not exist" );
 345  0
             }
 346  
         }
 347  
 
 348  0
         return properties;
 349  
     }
 350  
 
 351  
     private Properties removeDottedProperties( Properties properties )
 352  
     {
 353  0
         List<String> toRemove = new ArrayList<String>( 0 );
 354  
 
 355  0
         for ( Iterator<?> keys = properties.keySet().iterator(); keys.hasNext(); )
 356  
         {
 357  0
             String key = (String) keys.next();
 358  0
             if ( key.indexOf( "." ) >= 0 )
 359  
             {
 360  0
                 toRemove.add( key );
 361  
             }
 362  0
         }
 363  
 
 364  0
         for ( String key : toRemove )
 365  
         {
 366  0
             properties.remove( key );
 367  
         }
 368  0
         return properties;
 369  
     }
 370  
 }