Coverage Report - org.apache.maven.archetype.ui.DefaultArchetypeFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultArchetypeFactory
26%
28/107
15%
6/38
2.727
 
 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.Constants;
 23  
 import org.apache.maven.archetype.metadata.RequiredProperty;
 24  
 import org.apache.maven.project.MavenProject;
 25  
 
 26  
 import org.codehaus.plexus.component.annotations.Component;
 27  
 import org.codehaus.plexus.logging.AbstractLogEnabled;
 28  
 
 29  
 import java.util.Iterator;
 30  
 import java.util.Properties;
 31  
 
 32  
 @Component( role = ArchetypeFactory.class )
 33  6
 public class DefaultArchetypeFactory
 34  
     extends AbstractLogEnabled
 35  
     implements ArchetypeFactory
 36  
 {
 37  
     public ArchetypeDefinition createArchetypeDefinition( Properties properties )
 38  
     {
 39  0
         ArchetypeDefinition definition = new ArchetypeDefinition();
 40  
 
 41  0
         definition.setGroupId( properties.getProperty( Constants.ARCHETYPE_GROUP_ID ) );
 42  
 
 43  0
         definition.setArtifactId( properties.getProperty( Constants.ARCHETYPE_ARTIFACT_ID ) );
 44  
 
 45  0
         definition.setVersion( properties.getProperty( Constants.ARCHETYPE_VERSION ) );
 46  
 
 47  0
         definition.setRepository( properties.getProperty( Constants.ARCHETYPE_REPOSITORY ) );
 48  
 
 49  0
         definition.setUrl( properties.getProperty( Constants.ARCHETYPE_URL ) );
 50  
 
 51  0
         definition.setDescription( properties.getProperty( Constants.ARCHETYPE_DESCRIPTION ) );
 52  
 
 53  0
         return definition;
 54  
     }
 55  
 
 56  
     private void addOldRequiredProperty( ArchetypeConfiguration configuration, Properties properties, String key,
 57  
                                          String defaultValue, boolean initPropertyWithDefault )
 58  
     {
 59  24
         getLogger().debug( "Adding requiredProperty " + key );
 60  
 
 61  24
         configuration.addRequiredProperty( key );
 62  
 
 63  24
         String property = properties.getProperty( key );
 64  
 
 65  24
         if ( property != null )
 66  
         {
 67  14
             configuration.setProperty( key, property );
 68  14
             configuration.setDefaultProperty( key, property );
 69  
         }
 70  10
         else if ( defaultValue != null )
 71  
         {
 72  6
             if ( initPropertyWithDefault )
 73  
             {
 74  2
                 configuration.setProperty( key, defaultValue );
 75  
             }
 76  6
             configuration.setDefaultProperty( key, defaultValue );
 77  
         }
 78  
 
 79  24
         getLogger().debug( "Setting property " + key + "=" + configuration.getProperty( key ) );
 80  24
     }
 81  
 
 82  
     public ArchetypeConfiguration createArchetypeConfiguration( org.apache.maven.archetype.old.descriptor.ArchetypeDescriptor archetypeDescriptor,
 83  
                                                                 Properties properties )
 84  
     {
 85  6
         getLogger().debug( "Creating ArchetypeConfiguration from legacy descriptor and Properties" );
 86  
 
 87  6
         ArchetypeConfiguration configuration = createArchetypeConfiguration( properties );
 88  
 
 89  6
         configuration.setName( archetypeDescriptor.getId() );
 90  
 
 91  6
         addOldRequiredProperty( configuration, properties, Constants.GROUP_ID, null, false );
 92  
 
 93  6
         addOldRequiredProperty( configuration, properties, Constants.ARTIFACT_ID, null, false );
 94  
 
 95  6
         addOldRequiredProperty( configuration, properties, Constants.VERSION, "1.0-SNAPSHOT", false );
 96  
 
 97  6
         addOldRequiredProperty( configuration, properties, Constants.PACKAGE,
 98  
                                 configuration.getProperty( Constants.GROUP_ID ), true );
 99  
 
 100  6
         return configuration;
 101  
     }
 102  
 
 103  
     private void addRequiredProperty( ArchetypeConfiguration configuration, Properties properties, String key,
 104  
                                       String defaultValue, boolean initPropertyWithDefault )
 105  
     {
 106  0
         if ( !configuration.isConfigured( key ) && configuration.getDefaultValue( key ) == null )
 107  
         {
 108  0
             addOldRequiredProperty( configuration, properties, key, defaultValue, initPropertyWithDefault );
 109  
         }
 110  0
     }
 111  
 
 112  
     public ArchetypeConfiguration createArchetypeConfiguration( org.apache.maven.archetype.metadata.ArchetypeDescriptor archetypeDescriptor,
 113  
                                                                 Properties properties )
 114  
     {
 115  0
         getLogger().debug( "Creating ArchetypeConfiguration from fileset descriptor and Properties" );
 116  
 
 117  0
         ArchetypeConfiguration configuration = createArchetypeConfiguration( properties );
 118  
 
 119  0
         configuration.setName( archetypeDescriptor.getName() );
 120  
 
 121  0
         for ( RequiredProperty requiredProperty : archetypeDescriptor.getRequiredProperties() )
 122  
         {
 123  0
             String key = requiredProperty.getKey();
 124  0
             getLogger().debug( "Adding requiredProperty " + key );
 125  
 
 126  0
             configuration.addRequiredProperty( key );
 127  
 
 128  0
             String defaultValue = requiredProperty.getDefaultValue();
 129  
 
 130  0
             if ( properties.getProperty( key ) != null )
 131  
             {
 132  
                 // using value defined in properties, which overrides any default
 133  0
                 String value = properties.getProperty( key );
 134  0
                 configuration.setProperty( key, value );
 135  0
                 getLogger().debug( "Setting property " + key + "=" + value );
 136  0
             }
 137  0
             else if ( ( defaultValue != null ) && !containsInnerProperty( defaultValue ) )
 138  
             {
 139  
                 // using default value
 140  0
                  configuration.setProperty( key, defaultValue );
 141  0
                  getLogger().debug( "Setting property " + key + "=" + defaultValue );
 142  
             }
 143  
 
 144  0
             if ( defaultValue != null )
 145  
             {
 146  0
                 configuration.setDefaultProperty( key, defaultValue );
 147  0
                 getLogger().debug( "Setting defaultProperty " + key + "=" + defaultValue );
 148  
             }
 149  0
         }
 150  
 
 151  0
         addRequiredProperty( configuration, properties, Constants.GROUP_ID, null, false );
 152  
 
 153  0
         addRequiredProperty( configuration, properties, Constants.ARTIFACT_ID, null, false );
 154  
 
 155  0
         addRequiredProperty( configuration, properties, Constants.VERSION, "1.0-SNAPSHOT", false );
 156  
 
 157  0
         addRequiredProperty( configuration, properties, Constants.PACKAGE,
 158  
                              configuration.getProperty( Constants.GROUP_ID ), true );
 159  
 
 160  0
         String postGenerationGoals = properties.getProperty( Constants.ARCHETYPE_POST_GENERATION_GOALS );
 161  0
         if ( postGenerationGoals != null )
 162  
         {
 163  0
             configuration.setProperty( Constants.ARCHETYPE_POST_GENERATION_GOALS, postGenerationGoals );
 164  
         }
 165  
 
 166  0
         return configuration;
 167  
     }
 168  
 
 169  
     private void addRequiredProperty( ArchetypeConfiguration configuration, Properties properties, String key,
 170  
                                       String defaultValue )
 171  
     {
 172  0
         getLogger().debug( "Adding requiredProperty " + key );
 173  
 
 174  0
         configuration.addRequiredProperty( key );
 175  
 
 176  0
         if ( defaultValue != null )
 177  
         {
 178  0
             configuration.setDefaultProperty( key, defaultValue );
 179  
         }
 180  
 
 181  0
         if ( properties.getProperty( key ) != null )
 182  
         {
 183  0
             configuration.setProperty( key, properties.getProperty( key ) );
 184  
 
 185  0
             getLogger().debug( "Setting property " + key + "=" + configuration.getProperty( Constants.GROUP_ID ) );
 186  
         }
 187  0
     }
 188  
 
 189  
     private void setProperty( ArchetypeConfiguration configuration, Properties properties, String key )
 190  
     {
 191  0
         String property = properties.getProperty( key );
 192  
 
 193  0
         if ( property != null )
 194  
         {
 195  0
             configuration.setProperty( key, property );
 196  
         }
 197  0
     }
 198  
 
 199  
     public ArchetypeConfiguration createArchetypeConfiguration( MavenProject project,
 200  
                                                                 ArchetypeDefinition archetypeDefinition,
 201  
                                                                 Properties properties )
 202  
     {
 203  0
         getLogger().debug( "Creating ArchetypeConfiguration from ArchetypeDefinition, MavenProject and Properties" );
 204  
 
 205  0
         ArchetypeConfiguration configuration = createArchetypeConfiguration( properties );
 206  
 
 207  0
         for ( Iterator<?> requiredProperties = properties.keySet().iterator(); requiredProperties.hasNext(); )
 208  
         {
 209  0
             String requiredProperty = (String) requiredProperties.next();
 210  
 
 211  0
             if ( !requiredProperty.contains( "." ) )
 212  
             {
 213  0
                 getLogger().debug( "Adding requiredProperty " + requiredProperty );
 214  0
                 configuration.addRequiredProperty( requiredProperty );
 215  
 
 216  0
                 configuration.setProperty( requiredProperty, properties.getProperty( requiredProperty ) );
 217  0
                 getLogger().debug( "Setting property " + requiredProperty + "=" +
 218  
                                        configuration.getProperty( requiredProperty ) );
 219  
             }
 220  0
         }
 221  
 
 222  0
         addRequiredProperty( configuration, properties, Constants.GROUP_ID, project.getGroupId() );
 223  
 
 224  0
         addRequiredProperty( configuration, properties, Constants.ARTIFACT_ID, project.getArtifactId() );
 225  
 
 226  0
         addRequiredProperty( configuration, properties, Constants.VERSION, project.getVersion() );
 227  
 
 228  0
         addRequiredProperty( configuration, properties, Constants.PACKAGE, null );
 229  
 
 230  0
         setProperty( configuration, properties, Constants.ARCHETYPE_GROUP_ID );
 231  
 
 232  0
         setProperty( configuration, properties, Constants.ARCHETYPE_ARTIFACT_ID );
 233  
 
 234  0
         setProperty( configuration, properties, Constants.ARCHETYPE_VERSION );
 235  
 
 236  0
         setProperty( configuration, properties, Constants.ARCHETYPE_URL );
 237  
 
 238  0
         setProperty( configuration, properties, Constants.ARCHETYPE_DESCRIPTION );
 239  
 
 240  0
         return configuration;
 241  
     }
 242  
 
 243  
     private ArchetypeConfiguration createArchetypeConfiguration( Properties properties )
 244  
     {
 245  6
         ArchetypeConfiguration configuration = new ArchetypeConfiguration();
 246  
 
 247  6
         configuration.setGroupId( properties.getProperty( Constants.ARCHETYPE_GROUP_ID ) );
 248  
 
 249  6
         configuration.setArtifactId( properties.getProperty( Constants.ARCHETYPE_ARTIFACT_ID ) );
 250  
 
 251  6
         configuration.setVersion( properties.getProperty( Constants.ARCHETYPE_VERSION ) );
 252  
 
 253  6
         configuration.setUrl( properties.getProperty( Constants.ARCHETYPE_URL ) );
 254  
 
 255  6
         configuration.setDescription( properties.getProperty( Constants.ARCHETYPE_DESCRIPTION ) );
 256  
 
 257  6
         return configuration;
 258  
     }
 259  
 
 260  
     public void updateArchetypeConfiguration( ArchetypeConfiguration archetypeConfiguration,
 261  
                                               ArchetypeDefinition archetypeDefinition )
 262  
     {
 263  0
         archetypeConfiguration.setGroupId( archetypeDefinition.getGroupId() );
 264  0
         archetypeConfiguration.setArtifactId( archetypeDefinition.getArtifactId() );
 265  0
         archetypeConfiguration.setVersion( archetypeDefinition.getVersion() );
 266  0
     }
 267  
 
 268  
     /**
 269  
      * Check if the given value references a property, ie contains <code>${...}</code>.
 270  
      * 
 271  
      * @param defaultValue the value to check
 272  
      * @return <code>true</code> if the value contains <code>${</code> followed by <code>}</code>
 273  
      */
 274  
     private boolean containsInnerProperty( String defaultValue )
 275  
     {
 276  0
         if ( defaultValue == null )
 277  
         {
 278  0
             return false;
 279  
         }
 280  0
         int start = defaultValue.indexOf( "${" );
 281  0
         return ( start >= 0 ) && ( defaultValue.indexOf( "}", start ) >= 0 );
 282  
     }
 283  
 }