Coverage Report - org.apache.maven.archetype.ui.DefaultArchetypeFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultArchetypeFactory
23%
36/158
15%
9/62
5,833
 
 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.common.ArchetypeConfiguration;
 23  
 import org.apache.maven.archetype.common.ArchetypeDefinition;
 24  
 import org.apache.maven.archetype.common.Constants;
 25  
 import org.apache.maven.project.MavenProject;
 26  
 
 27  
 import org.codehaus.plexus.logging.AbstractLogEnabled;
 28  
 
 29  
 import java.util.Iterator;
 30  
 import java.util.Properties;
 31  
 
 32  
 /**
 33  
  * @plexus.component
 34  
  */
 35  9
 public class DefaultArchetypeFactory
 36  
 extends AbstractLogEnabled
 37  
 implements ArchetypeFactory
 38  
 {
 39  
     public ArchetypeConfiguration createArchetypeConfiguration( ArchetypeDefinition archetypeDefinition,
 40  
         Properties properties )
 41  
     {
 42  0
         ArchetypeConfiguration configuration = new ArchetypeConfiguration();
 43  0
         getLogger().debug( "Creating ArchetypeConfiguration from ArchetypeDefinition and Properties" );
 44  
 
 45  0
         configuration.setGroupId( archetypeDefinition.getGroupId() );
 46  0
         configuration.setArtifactId( archetypeDefinition.getArtifactId() );
 47  0
         configuration.setVersion( archetypeDefinition.getVersion() );
 48  
 
 49  0
         Iterator propertiesIterator = properties.keySet().iterator();
 50  0
         while( propertiesIterator.hasNext() )
 51  
         {
 52  0
             String property = (String) propertiesIterator.next();
 53  0
             if( !Constants.ARCHETYPE_GROUP_ID.equals( property ) && !Constants.ARCHETYPE_ARTIFACT_ID.equals( property )
 54  
                 && !Constants.ARCHETYPE_VERSION.equals( property ) )
 55  
             {
 56  0
                 configuration.addRequiredProperty( property );
 57  
 
 58  0
                 getLogger().debug( "Adding requiredProperty " + property );
 59  
 
 60  0
                 configuration.setProperty( property, properties.getProperty( property ) );
 61  
 
 62  0
                 getLogger().debug( "Adding property " + property + "=" + properties.getProperty( property ) );
 63  
             }
 64  0
         }
 65  
 
 66  0
         return configuration;
 67  
     }
 68  
 
 69  
     public ArchetypeConfiguration createArchetypeConfiguration(
 70  
         org.apache.maven.archetype.old.descriptor.ArchetypeDescriptor archetypeDescriptor, Properties properties )
 71  
     {
 72  9
         ArchetypeConfiguration configuration = new ArchetypeConfiguration();
 73  9
         getLogger().debug( "Creating ArchetypeConfiguration from legacy descriptor and Properties" );
 74  
 
 75  9
         configuration.setGroupId( properties.getProperty( Constants.ARCHETYPE_GROUP_ID, null ) );
 76  9
         configuration.setArtifactId( properties.getProperty( Constants.ARCHETYPE_ARTIFACT_ID, null ) );
 77  9
         configuration.setVersion( properties.getProperty( Constants.ARCHETYPE_VERSION, null ) );
 78  
 
 79  9
         configuration.setName( archetypeDescriptor.getId() );
 80  
 
 81  9
         configuration.addRequiredProperty( Constants.GROUP_ID );
 82  9
         getLogger().debug( "Adding requiredProperty " + Constants.GROUP_ID );
 83  9
         if( null != properties.getProperty( Constants.GROUP_ID ) )
 84  
         {
 85  6
             configuration.setProperty( Constants.GROUP_ID, properties.getProperty( Constants.GROUP_ID ) );
 86  6
             configuration.setDefaultProperty( Constants.GROUP_ID, configuration.getProperty( Constants.GROUP_ID ) );
 87  
         }
 88  9
         getLogger().debug( "Setting property " + Constants.GROUP_ID + "="
 89  
             + configuration.getProperty( Constants.GROUP_ID ) );
 90  
 
 91  9
         configuration.addRequiredProperty( Constants.ARTIFACT_ID );
 92  9
         getLogger().debug( "Adding requiredProperty " + Constants.ARTIFACT_ID );
 93  9
         if( null != properties.getProperty( Constants.ARTIFACT_ID ) )
 94  
         {
 95  9
             configuration.setProperty( Constants.ARTIFACT_ID, properties.getProperty( Constants.ARTIFACT_ID ) );
 96  9
             configuration.setDefaultProperty( Constants.ARTIFACT_ID,
 97  
                 configuration.getProperty( Constants.ARTIFACT_ID ) );
 98  
         }
 99  9
         getLogger().debug( "Setting property " + Constants.ARTIFACT_ID + "="
 100  
             + configuration.getProperty( Constants.ARTIFACT_ID ) );
 101  
 
 102  9
         configuration.addRequiredProperty( Constants.VERSION );
 103  9
         getLogger().debug( "Adding requiredProperty " + Constants.VERSION );
 104  9
         if( null != properties.getProperty( Constants.VERSION ) )
 105  
         {
 106  3
             configuration.setProperty( Constants.VERSION, properties.getProperty( Constants.VERSION ) );
 107  3
             configuration.setDefaultProperty( Constants.VERSION, configuration.getProperty( Constants.VERSION ) );
 108  
         }
 109  
         else
 110  
         {
 111  6
             configuration.setDefaultProperty( Constants.VERSION, "1.0-SNAPSHOT" );
 112  
         }
 113  9
         getLogger().debug( "Setting property " + Constants.VERSION + "="
 114  
             + configuration.getProperty( Constants.VERSION ) );
 115  
 
 116  9
         configuration.addRequiredProperty( Constants.PACKAGE );
 117  9
         getLogger().debug( "Adding requiredProperty " + Constants.PACKAGE );
 118  9
         if( null != properties.getProperty( Constants.PACKAGE ) )
 119  
         {
 120  3
             configuration.setProperty( Constants.PACKAGE, properties.getProperty( Constants.PACKAGE ) );
 121  3
             configuration.setDefaultProperty( Constants.PACKAGE, configuration.getProperty( Constants.PACKAGE ) );
 122  
         }
 123  6
         else if( null != configuration.getProperty( Constants.GROUP_ID ) )
 124  
         {
 125  3
             configuration.setProperty( Constants.PACKAGE, configuration.getProperty( Constants.GROUP_ID ) );
 126  3
             configuration.setDefaultProperty( Constants.PACKAGE, configuration.getProperty( Constants.PACKAGE ) );
 127  
         }
 128  9
         getLogger().debug( "Setting property " + Constants.PACKAGE + "="
 129  
             + configuration.getProperty( Constants.PACKAGE ) );
 130  
 
 131  9
         return configuration;
 132  
     }
 133  
 
 134  
     public ArchetypeConfiguration createArchetypeConfiguration(
 135  
         org.apache.maven.archetype.metadata.ArchetypeDescriptor archetypeDescriptor, Properties properties )
 136  
     {
 137  0
         ArchetypeConfiguration configuration = new ArchetypeConfiguration();
 138  0
         getLogger().debug( "Creating ArchetypeConfiguration from fileset descriptor and Properties" );
 139  
 
 140  0
         configuration.setGroupId( properties.getProperty( Constants.ARCHETYPE_GROUP_ID, null ) );
 141  0
         configuration.setArtifactId( properties.getProperty( Constants.ARCHETYPE_ARTIFACT_ID, null ) );
 142  0
         configuration.setVersion( properties.getProperty( Constants.ARCHETYPE_VERSION, null ) );
 143  
 
 144  0
         configuration.setName( archetypeDescriptor.getName() );
 145  
 
 146  0
         Iterator requiredProperties = archetypeDescriptor.getRequiredProperties().iterator();
 147  
 
 148  0
         while( requiredProperties.hasNext() )
 149  
         {
 150  0
             org.apache.maven.archetype.metadata.RequiredProperty requiredProperty =
 151  
                 (org.apache.maven.archetype.metadata.RequiredProperty) requiredProperties.next();
 152  
 
 153  0
             configuration.addRequiredProperty( requiredProperty.getKey() );
 154  0
             getLogger().debug( "Adding requiredProperty " + requiredProperty.getKey() );
 155  
 
 156  0
             if( null != properties.getProperty( requiredProperty.getKey(), requiredProperty.getDefaultValue() ) )
 157  
             {
 158  0
                 configuration.setProperty( requiredProperty.getKey(),
 159  
                     properties.getProperty( requiredProperty.getKey(), requiredProperty.getDefaultValue() ) );
 160  0
                 getLogger().debug( "Setting property " + requiredProperty.getKey() + "="
 161  
                     + configuration.getProperty( requiredProperty.getKey() ) );
 162  
             }
 163  0
             if( null != requiredProperty.getDefaultValue() )
 164  
             {
 165  0
                 configuration.setDefaultProperty( requiredProperty.getKey(), requiredProperty.getDefaultValue() );
 166  0
                 getLogger().debug( "Setting defaultProperty " + requiredProperty.getKey() + "="
 167  
                     + configuration.getDefaultValue( requiredProperty.getKey() ) );
 168  
             }
 169  0
         } // end while
 170  
 
 171  0
         if( !configuration.isConfigured( Constants.GROUP_ID ) )
 172  
         {
 173  0
             configuration.addRequiredProperty( Constants.GROUP_ID );
 174  0
             getLogger().debug( "Adding requiredProperty " + Constants.GROUP_ID );
 175  0
             if( null
 176  
                 != properties.getProperty( Constants.GROUP_ID, configuration.getDefaultValue( Constants.GROUP_ID ) ) )
 177  
             {
 178  0
                 configuration.setProperty( Constants.GROUP_ID,
 179  
                     properties.getProperty( Constants.GROUP_ID, configuration.getDefaultValue( Constants.GROUP_ID ) ) );
 180  0
                 configuration.setDefaultProperty( Constants.GROUP_ID, configuration.getProperty( Constants.GROUP_ID ) );
 181  
             }
 182  0
             getLogger().debug( "Setting property " + Constants.GROUP_ID + "="
 183  
                 + configuration.getProperty( Constants.GROUP_ID ) );
 184  
         }
 185  0
         if( !configuration.isConfigured( Constants.ARTIFACT_ID ) )
 186  
         {
 187  0
             configuration.addRequiredProperty( Constants.ARTIFACT_ID );
 188  0
             getLogger().debug( "Adding requiredProperty " + Constants.ARTIFACT_ID );
 189  0
             if( null
 190  
                 != properties.getProperty( Constants.ARTIFACT_ID,
 191  
                     configuration.getDefaultValue( Constants.ARTIFACT_ID ) ) )
 192  
             {
 193  0
                 configuration.setProperty( Constants.ARTIFACT_ID, properties.getProperty( Constants.ARTIFACT_ID ) );
 194  0
                 configuration.setDefaultProperty( Constants.ARTIFACT_ID,
 195  
                     configuration.getProperty( Constants.ARTIFACT_ID ) );
 196  
             }
 197  0
             getLogger().debug( "Setting property " + Constants.ARTIFACT_ID + "="
 198  
                 + configuration.getProperty( Constants.ARTIFACT_ID ) );
 199  
         }
 200  0
         if( !configuration.isConfigured( Constants.VERSION ) )
 201  
         {
 202  0
             configuration.addRequiredProperty( Constants.VERSION );
 203  0
             getLogger().debug( "Adding requiredProperty " + Constants.VERSION );
 204  0
             if( null != properties.getProperty( Constants.VERSION,
 205  
                     configuration.getDefaultValue( Constants.VERSION ) ) )
 206  
             {
 207  0
                 configuration.setProperty( Constants.VERSION,
 208  
                     properties.getProperty( Constants.VERSION, configuration.getDefaultValue( Constants.VERSION ) ) );
 209  0
                 configuration.setDefaultProperty( Constants.VERSION, configuration.getProperty( Constants.VERSION ) );
 210  
             }
 211  
             else
 212  
             {
 213  0
                 configuration.setDefaultProperty( Constants.VERSION, "1.0-SNAPSHOT" );
 214  
             }
 215  0
             getLogger().debug( "Setting property " + Constants.VERSION + "="
 216  
                 + configuration.getProperty( Constants.VERSION ) );
 217  
         }
 218  0
         if( !configuration.isConfigured( Constants.PACKAGE ) )
 219  
         {
 220  0
             configuration.addRequiredProperty( Constants.PACKAGE );
 221  0
             getLogger().debug( "Adding requiredProperty " + Constants.PACKAGE );
 222  0
             if( null != properties.getProperty( Constants.PACKAGE,
 223  
                     configuration.getDefaultValue( Constants.PACKAGE ) ) )
 224  
             {
 225  0
                 configuration.setProperty( Constants.PACKAGE,
 226  
                     properties.getProperty( Constants.PACKAGE, configuration.getDefaultValue( Constants.PACKAGE ) ) );
 227  0
                 configuration.setDefaultProperty( Constants.PACKAGE, configuration.getProperty( Constants.PACKAGE ) );
 228  
             }
 229  0
             else if( null != configuration.getProperty( Constants.GROUP_ID ) )
 230  
             {
 231  0
                 configuration.setProperty( Constants.PACKAGE, configuration.getProperty( Constants.GROUP_ID ) );
 232  0
                 configuration.setDefaultProperty( Constants.PACKAGE, configuration.getProperty( Constants.PACKAGE ) );
 233  
             }
 234  0
             getLogger().debug( "Setting property " + Constants.PACKAGE + "="
 235  
                 + configuration.getProperty( Constants.PACKAGE ) );
 236  
         }
 237  
 
 238  0
         if( null != properties.getProperty( Constants.ARCHETYPE_POST_GENERATION_GOALS ) )
 239  
         {
 240  0
             configuration.setProperty( Constants.ARCHETYPE_POST_GENERATION_GOALS,
 241  
                 properties.getProperty( Constants.ARCHETYPE_POST_GENERATION_GOALS ) );
 242  
         }
 243  
 
 244  0
         return configuration;
 245  
     }
 246  
 
 247  
     public ArchetypeConfiguration createArchetypeConfiguration( MavenProject project,
 248  
         ArchetypeDefinition archetypeDefinition, Properties properties )
 249  
     {
 250  0
         ArchetypeConfiguration configuration = new ArchetypeConfiguration();
 251  0
         getLogger().debug( "Creating ArchetypeConfiguration from ArchetypeDefinition, MavenProject and Properties" );
 252  
 
 253  0
         configuration.setGroupId( properties.getProperty(Constants.ARCHETYPE_GROUP_ID) );
 254  0
         configuration.setArtifactId( properties.getProperty(Constants.ARCHETYPE_ARTIFACT_ID) );
 255  0
         configuration.setVersion( properties.getProperty(Constants.ARCHETYPE_VERSION) );
 256  
 
 257  0
         Iterator requiredProperties = properties.keySet().iterator();
 258  
 
 259  0
         while( requiredProperties.hasNext() )
 260  
         {
 261  0
             String requiredProperty = (String) requiredProperties.next();
 262  
 
 263  0
             if( requiredProperty.indexOf( "." ) < 0 )
 264  
             {
 265  0
                 configuration.addRequiredProperty( requiredProperty );
 266  0
                 getLogger().debug( "Adding requiredProperty " + requiredProperty );
 267  0
                 configuration.setProperty( requiredProperty, properties.getProperty( requiredProperty ) );
 268  0
                 getLogger().debug( "Setting property " + requiredProperty + "="
 269  
                     + configuration.getProperty( requiredProperty ) );
 270  
             }
 271  0
         }
 272  
 
 273  0
         configuration.addRequiredProperty( Constants.GROUP_ID );
 274  0
         getLogger().debug( "Adding requiredProperty " + Constants.GROUP_ID );
 275  0
         configuration.setDefaultProperty( Constants.GROUP_ID, project.getGroupId() );
 276  0
         if( null != properties.getProperty( Constants.GROUP_ID, null ) )
 277  
         {
 278  0
             configuration.setProperty( Constants.GROUP_ID, properties.getProperty( Constants.GROUP_ID ) );
 279  0
             getLogger().debug( "Setting property " + Constants.GROUP_ID + "="
 280  
                 + configuration.getProperty( Constants.GROUP_ID ) );
 281  
         }
 282  
 
 283  0
         configuration.addRequiredProperty( Constants.ARTIFACT_ID );
 284  0
         getLogger().debug( "Adding requiredProperty " + Constants.ARTIFACT_ID );
 285  0
         configuration.setDefaultProperty( Constants.ARTIFACT_ID, project.getArtifactId() );
 286  0
         if( null != properties.getProperty( Constants.ARTIFACT_ID, null ) )
 287  
         {
 288  0
             configuration.setProperty( Constants.ARTIFACT_ID, properties.getProperty( Constants.ARTIFACT_ID ) );
 289  0
             getLogger().debug( "Setting property " + Constants.ARTIFACT_ID + "="
 290  
                 + configuration.getProperty( Constants.ARTIFACT_ID ) );
 291  
         }
 292  
 
 293  0
         configuration.addRequiredProperty( Constants.VERSION );
 294  0
         getLogger().debug( "Adding requiredProperty " + Constants.VERSION );
 295  0
         configuration.setDefaultProperty( Constants.VERSION, project.getVersion() );
 296  0
         if( null != properties.getProperty( Constants.VERSION, null ) )
 297  
         {
 298  0
             configuration.setProperty( Constants.VERSION, properties.getProperty( Constants.VERSION ) );
 299  0
             getLogger().debug( "Setting property " + Constants.VERSION + "="
 300  
                 + configuration.getProperty( Constants.VERSION ) );
 301  
         }
 302  
 
 303  0
         configuration.addRequiredProperty( Constants.PACKAGE );
 304  0
         getLogger().debug( "Adding requiredProperty " + Constants.PACKAGE );
 305  0
         if( null != properties.getProperty( Constants.PACKAGE ) )
 306  
         {
 307  0
             configuration.setProperty( Constants.PACKAGE, properties.getProperty( Constants.PACKAGE ) );
 308  
 
 309  0
             getLogger().debug( "Setting property " + Constants.PACKAGE + "="
 310  
                 + configuration.getProperty( Constants.PACKAGE ) );
 311  
         }
 312  
 
 313  0
         if( null != properties.getProperty( Constants.ARCHETYPE_GROUP_ID, null ) )
 314  
         {
 315  0
             configuration.setProperty( Constants.ARCHETYPE_GROUP_ID, properties.getProperty( Constants.ARCHETYPE_GROUP_ID ) );
 316  
         }
 317  
 
 318  0
         if( null != properties.getProperty( Constants.ARCHETYPE_ARTIFACT_ID, null ) )
 319  
         {
 320  0
             configuration.setProperty( Constants.ARCHETYPE_ARTIFACT_ID, properties.getProperty( Constants.ARCHETYPE_ARTIFACT_ID ) );
 321  
         }
 322  
 
 323  0
         if( null != properties.getProperty( Constants.ARCHETYPE_VERSION, null ) )
 324  
         {
 325  0
             configuration.setProperty( Constants.ARCHETYPE_VERSION, properties.getProperty( Constants.ARCHETYPE_VERSION ) );
 326  
         }
 327  0
         return configuration;
 328  
     }
 329  
 
 330  
     public ArchetypeDefinition createArchetypeDefinition( Properties properties )
 331  
     {
 332  0
         ArchetypeDefinition definition = new ArchetypeDefinition();
 333  
 
 334  0
         definition.setGroupId( properties.getProperty( Constants.ARCHETYPE_GROUP_ID, null ) );
 335  
 
 336  0
         definition.setArtifactId( properties.getProperty( Constants.ARCHETYPE_ARTIFACT_ID, null ) );
 337  
 
 338  0
         definition.setVersion( properties.getProperty( Constants.ARCHETYPE_VERSION, null ) );
 339  
 
 340  0
         definition.setRepository( properties.getProperty( Constants.ARCHETYPE_REPOSITORY, null ) );
 341  
 
 342  0
         return definition;
 343  
     }
 344  
 
 345  
     public void updateArchetypeConfiguration( ArchetypeConfiguration archetypeConfiguration,
 346  
         ArchetypeDefinition archetypeDefinition )
 347  
     {
 348  0
         archetypeConfiguration.setGroupId( archetypeDefinition.getGroupId() );
 349  0
         archetypeConfiguration.setArtifactId( archetypeDefinition.getArtifactId() );
 350  0
         archetypeConfiguration.setVersion( archetypeDefinition.getVersion() );
 351  0
     }
 352  
 }