View Javadoc

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