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