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