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          Iterator propertiesIterator = properties.keySet().iterator();
50          while( propertiesIterator.hasNext() )
51          {
52              String property = (String) propertiesIterator.next();
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, configuration.getProperty( Constants.ARTIFACT_ID ) );
97          }
98          getLogger().debug( "Setting property " + Constants.ARTIFACT_ID + "="
99              + configuration.getProperty( Constants.ARTIFACT_ID ) );
100 
101         configuration.addRequiredProperty( Constants.VERSION );
102         getLogger().debug( "Adding requiredProperty " + Constants.VERSION );
103         if( null != properties.getProperty( Constants.VERSION ) )
104         {
105             configuration.setProperty( Constants.VERSION, properties.getProperty( Constants.VERSION ) );
106         }
107         else
108         {
109             configuration.setProperty( Constants.VERSION, "1.0-SNAPSHOT" );
110         }
111         configuration.setDefaultProperty( Constants.VERSION, configuration.getProperty( Constants.VERSION ) );
112         getLogger().debug( "Setting property " + Constants.VERSION + "="
113             + configuration.getProperty( Constants.VERSION ) );
114 
115         configuration.addRequiredProperty( Constants.PACKAGE );
116         getLogger().debug( "Adding requiredProperty " + Constants.PACKAGE );
117         if( null != properties.getProperty( Constants.PACKAGE ) )
118         {
119             configuration.setProperty( Constants.PACKAGE, properties.getProperty( Constants.PACKAGE ) );
120             configuration.setDefaultProperty( Constants.PACKAGE, configuration.getProperty( Constants.PACKAGE ) );
121         }
122         else if( null != configuration.getProperty( Constants.GROUP_ID ) )
123         {
124             configuration.setProperty( Constants.PACKAGE, configuration.getProperty( Constants.GROUP_ID ) );
125             configuration.setDefaultProperty( Constants.PACKAGE, configuration.getProperty( Constants.PACKAGE ) );
126         }
127         getLogger().debug( "Setting property " + Constants.PACKAGE + "="
128             + configuration.getProperty( Constants.PACKAGE ) );
129 
130         return configuration;
131     }
132 
133     public ArchetypeConfiguration createArchetypeConfiguration(
134         org.apache.maven.archetype.metadata.ArchetypeDescriptor archetypeDescriptor, Properties properties )
135     {
136         ArchetypeConfiguration configuration = new ArchetypeConfiguration();
137         getLogger().debug( "Creating ArchetypeConfiguration from fileset descriptor and Properties" );
138 
139         configuration.setGroupId( properties.getProperty( Constants.ARCHETYPE_GROUP_ID, null ) );
140         configuration.setArtifactId( properties.getProperty( Constants.ARCHETYPE_ARTIFACT_ID, null ) );
141         configuration.setVersion( properties.getProperty( Constants.ARCHETYPE_VERSION, null ) );
142 
143         configuration.setName( archetypeDescriptor.getName() );
144 
145         Iterator requiredProperties = archetypeDescriptor.getRequiredProperties().iterator();
146 
147         while( 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             {
157                 configuration.setProperty( requiredProperty.getKey(),
158                     properties.getProperty( requiredProperty.getKey(), requiredProperty.getDefaultValue() ) );
159                 getLogger().debug( "Setting property " + requiredProperty.getKey() + "="
160                     + configuration.getProperty( requiredProperty.getKey() ) );
161             }
162             if( null != requiredProperty.getDefaultValue() )
163             {
164                 configuration.setDefaultProperty( requiredProperty.getKey(), requiredProperty.getDefaultValue() );
165                 getLogger().debug( "Setting defaultProperty " + requiredProperty.getKey() + "="
166                     + configuration.getDefaultValue( requiredProperty.getKey() ) );
167             }
168         } // end while
169 
170         if( !configuration.isConfigured( Constants.GROUP_ID ) )
171         {
172             configuration.addRequiredProperty( Constants.GROUP_ID );
173             getLogger().debug( "Adding requiredProperty " + Constants.GROUP_ID );
174             if( null
175                 != properties.getProperty( Constants.GROUP_ID, configuration.getDefaultValue( Constants.GROUP_ID ) ) )
176             {
177                 configuration.setProperty( Constants.GROUP_ID,
178                     properties.getProperty( Constants.GROUP_ID, configuration.getDefaultValue( Constants.GROUP_ID ) ) );
179                 configuration.setDefaultProperty( Constants.GROUP_ID, configuration.getProperty( Constants.GROUP_ID ) );
180             }
181             getLogger().debug( "Setting property " + Constants.GROUP_ID + "="
182                 + configuration.getProperty( Constants.GROUP_ID ) );
183         }
184         if( !configuration.isConfigured( Constants.ARTIFACT_ID ) )
185         {
186             configuration.addRequiredProperty( Constants.ARTIFACT_ID );
187             getLogger().debug( "Adding requiredProperty " + Constants.ARTIFACT_ID );
188             if( null
189                 != properties.getProperty( Constants.ARTIFACT_ID,
190                     configuration.getDefaultValue( Constants.ARTIFACT_ID ) ) )
191             {
192                 configuration.setProperty( Constants.ARTIFACT_ID, properties.getProperty( Constants.ARTIFACT_ID ) );
193                 configuration.setDefaultProperty( Constants.ARTIFACT_ID, configuration.getProperty( Constants.ARTIFACT_ID ) );
194             }
195             getLogger().debug( "Setting property " + Constants.ARTIFACT_ID + "="
196                 + configuration.getProperty( Constants.ARTIFACT_ID ) );
197         }
198         if( !configuration.isConfigured( Constants.VERSION ) )
199         {
200             configuration.addRequiredProperty( Constants.VERSION );
201             getLogger().debug( "Adding requiredProperty " + Constants.VERSION );
202             if( null != properties.getProperty( Constants.VERSION,
203                     configuration.getDefaultValue( Constants.VERSION ) ) )
204             {
205                 configuration.setProperty( Constants.VERSION,
206                     properties.getProperty( Constants.VERSION, configuration.getDefaultValue( Constants.VERSION ) ) );
207             }
208             else
209             {
210                 configuration.setProperty( Constants.VERSION, "1.0-SNAPSHOT" );
211             }
212             configuration.setDefaultProperty( Constants.VERSION, configuration.getProperty( Constants.VERSION ) );
213             getLogger().debug( "Setting property " + Constants.VERSION + "="
214                 + configuration.getProperty( Constants.VERSION ) );
215         }
216         if( !configuration.isConfigured( Constants.PACKAGE ) )
217         {
218             configuration.addRequiredProperty( Constants.PACKAGE );
219             getLogger().debug( "Adding requiredProperty " + Constants.PACKAGE );
220             if( null != properties.getProperty( Constants.PACKAGE,
221                     configuration.getDefaultValue( Constants.PACKAGE ) ) )
222             {
223                 configuration.setProperty( Constants.PACKAGE,
224                     properties.getProperty( Constants.PACKAGE, configuration.getDefaultValue( Constants.PACKAGE ) ) );
225                 configuration.setDefaultProperty( Constants.PACKAGE, configuration.getProperty( Constants.PACKAGE ) );
226             }
227             else if( null != configuration.getProperty( Constants.GROUP_ID ) )
228             {
229                 configuration.setProperty( Constants.PACKAGE, configuration.getProperty( Constants.GROUP_ID ) );
230                 configuration.setDefaultProperty( Constants.PACKAGE, configuration.getProperty( Constants.PACKAGE ) );
231             }
232             getLogger().debug( "Setting property " + Constants.PACKAGE + "="
233                 + configuration.getProperty( Constants.PACKAGE ) );
234         }
235 
236         if( null != properties.getProperty( Constants.ARCHETYPE_POST_GENERATION_GOALS ) )
237         {
238             configuration.setProperty( Constants.ARCHETYPE_POST_GENERATION_GOALS,
239                 properties.getProperty( Constants.ARCHETYPE_POST_GENERATION_GOALS ) );
240         }
241 
242         return configuration;
243     }
244 
245     public ArchetypeConfiguration createArchetypeConfiguration( MavenProject project,
246         ArchetypeDefinition archetypeDefinition, Properties properties )
247     {
248         ArchetypeConfiguration configuration = new ArchetypeConfiguration();
249         getLogger().debug( "Creating ArchetypeConfiguration from ArchetypeDefinition, MavenProject and Properties" );
250 
251         configuration.setGroupId( archetypeDefinition.getGroupId() );
252         configuration.setArtifactId( archetypeDefinition.getArtifactId() );
253         configuration.setVersion( archetypeDefinition.getVersion() );
254 
255         Iterator requiredProperties = properties.keySet().iterator();
256 
257         while( requiredProperties.hasNext() )
258         {
259             String requiredProperty = (String) requiredProperties.next();
260 
261             if( requiredProperty.indexOf( "." ) < 0 )
262             {
263                 configuration.addRequiredProperty( requiredProperty );
264                 getLogger().debug( "Adding requiredProperty " + requiredProperty );
265                 configuration.setProperty( requiredProperty, properties.getProperty( requiredProperty ) );
266                 getLogger().debug( "Setting property " + requiredProperty + "="
267                     + configuration.getProperty( requiredProperty ) );
268             }
269         }
270 
271         configuration.addRequiredProperty( Constants.GROUP_ID );
272         getLogger().debug( "Adding requiredProperty " + Constants.GROUP_ID );
273         configuration.setDefaultProperty( Constants.GROUP_ID, project.getGroupId() );
274         if( null != properties.getProperty( Constants.GROUP_ID, null ) )
275         {
276             configuration.setProperty( Constants.GROUP_ID, properties.getProperty( Constants.GROUP_ID ) );
277             getLogger().debug( "Setting property " + Constants.GROUP_ID + "="
278                 + configuration.getProperty( Constants.GROUP_ID ) );
279         }
280 
281         configuration.addRequiredProperty( Constants.ARTIFACT_ID );
282         getLogger().debug( "Adding requiredProperty " + Constants.ARTIFACT_ID );
283         configuration.setDefaultProperty( Constants.ARTIFACT_ID, project.getArtifactId() );
284         if( null != properties.getProperty( Constants.ARTIFACT_ID, null ) )
285         {
286             configuration.setProperty( Constants.ARTIFACT_ID, properties.getProperty( Constants.ARTIFACT_ID ) );
287             getLogger().debug( "Setting property " + Constants.ARTIFACT_ID + "="
288                 + configuration.getProperty( Constants.ARTIFACT_ID ) );
289         }
290 
291         configuration.addRequiredProperty( Constants.VERSION );
292         getLogger().debug( "Adding requiredProperty " + Constants.VERSION );
293         configuration.setDefaultProperty( Constants.VERSION, project.getVersion() );
294         if( null != properties.getProperty( Constants.VERSION, null ) )
295         {
296             configuration.setProperty( Constants.VERSION, properties.getProperty( Constants.VERSION ) );
297             getLogger().debug( "Setting property " + Constants.VERSION + "="
298                 + configuration.getProperty( Constants.VERSION ) );
299         }
300 
301         configuration.addRequiredProperty( Constants.PACKAGE );
302         getLogger().debug( "Adding requiredProperty " + Constants.PACKAGE );
303         if( null != properties.getProperty( Constants.PACKAGE ) )
304         {
305             configuration.setProperty( Constants.PACKAGE, properties.getProperty( Constants.PACKAGE ) );
306 
307             getLogger().debug( "Setting property " + Constants.PACKAGE + "="
308                 + configuration.getProperty( Constants.PACKAGE ) );
309         }
310 
311         return configuration;
312     }
313 
314     public ArchetypeDefinition createArchetypeDefinition( Properties properties )
315     {
316         ArchetypeDefinition definition = new ArchetypeDefinition();
317 
318         definition.setGroupId( properties.getProperty( Constants.ARCHETYPE_GROUP_ID, null ) );
319 
320         definition.setArtifactId( properties.getProperty( Constants.ARCHETYPE_ARTIFACT_ID, null ) );
321 
322         definition.setVersion( properties.getProperty( Constants.ARCHETYPE_VERSION, null ) );
323 
324         definition.setRepository( properties.getProperty( Constants.ARCHETYPE_REPOSITORY, null ) );
325 
326         return definition;
327     }
328 
329     public void updateArchetypeConfiguration( ArchetypeConfiguration archetypeConfiguration,
330         ArchetypeDefinition archetypeDefinition )
331     {
332         archetypeConfiguration.setGroupId( archetypeDefinition.getGroupId() );
333         archetypeConfiguration.setArtifactId( archetypeDefinition.getArtifactId() );
334         archetypeConfiguration.setVersion( archetypeDefinition.getVersion() );
335     }
336 }