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,
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         Iterator requiredProperties = archetypeDescriptor.getRequiredProperties().iterator();
147 
148         while( requiredProperties.hasNext() )
149         {
150             org.apache.maven.archetype.metadata.RequiredProperty requiredProperty =
151                 (org.apache.maven.archetype.metadata.RequiredProperty) requiredProperties.next();
152 
153             configuration.addRequiredProperty( requiredProperty.getKey() );
154             getLogger().debug( "Adding requiredProperty " + requiredProperty.getKey() );
155 
156             if( null != properties.getProperty( requiredProperty.getKey(), 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         } // end while
170 
171         if( !configuration.isConfigured( Constants.GROUP_ID ) )
172         {
173             configuration.addRequiredProperty( Constants.GROUP_ID );
174             getLogger().debug( "Adding requiredProperty " + Constants.GROUP_ID );
175             if( null
176                 != properties.getProperty( Constants.GROUP_ID, configuration.getDefaultValue( Constants.GROUP_ID ) ) )
177             {
178                 configuration.setProperty( Constants.GROUP_ID,
179                     properties.getProperty( Constants.GROUP_ID, configuration.getDefaultValue( Constants.GROUP_ID ) ) );
180                 configuration.setDefaultProperty( Constants.GROUP_ID, configuration.getProperty( Constants.GROUP_ID ) );
181             }
182             getLogger().debug( "Setting property " + Constants.GROUP_ID + "="
183                 + configuration.getProperty( Constants.GROUP_ID ) );
184         }
185         if( !configuration.isConfigured( Constants.ARTIFACT_ID ) )
186         {
187             configuration.addRequiredProperty( Constants.ARTIFACT_ID );
188             getLogger().debug( "Adding requiredProperty " + Constants.ARTIFACT_ID );
189             if( null
190                 != properties.getProperty( Constants.ARTIFACT_ID,
191                     configuration.getDefaultValue( Constants.ARTIFACT_ID ) ) )
192             {
193                 configuration.setProperty( Constants.ARTIFACT_ID, properties.getProperty( Constants.ARTIFACT_ID ) );
194                 configuration.setDefaultProperty( Constants.ARTIFACT_ID,
195                     configuration.getProperty( Constants.ARTIFACT_ID ) );
196             }
197             getLogger().debug( "Setting property " + Constants.ARTIFACT_ID + "="
198                 + configuration.getProperty( Constants.ARTIFACT_ID ) );
199         }
200         if( !configuration.isConfigured( Constants.VERSION ) )
201         {
202             configuration.addRequiredProperty( Constants.VERSION );
203             getLogger().debug( "Adding requiredProperty " + Constants.VERSION );
204             if( null != properties.getProperty( Constants.VERSION,
205                     configuration.getDefaultValue( Constants.VERSION ) ) )
206             {
207                 configuration.setProperty( Constants.VERSION,
208                     properties.getProperty( Constants.VERSION, configuration.getDefaultValue( Constants.VERSION ) ) );
209                 configuration.setDefaultProperty( Constants.VERSION, configuration.getProperty( Constants.VERSION ) );
210             }
211             else
212             {
213                 configuration.setDefaultProperty( Constants.VERSION, "1.0-SNAPSHOT" );
214             }
215             getLogger().debug( "Setting property " + Constants.VERSION + "="
216                 + configuration.getProperty( Constants.VERSION ) );
217         }
218         if( !configuration.isConfigured( Constants.PACKAGE ) )
219         {
220             configuration.addRequiredProperty( Constants.PACKAGE );
221             getLogger().debug( "Adding requiredProperty " + Constants.PACKAGE );
222             if( null != properties.getProperty( Constants.PACKAGE,
223                     configuration.getDefaultValue( Constants.PACKAGE ) ) )
224             {
225                 configuration.setProperty( Constants.PACKAGE,
226                     properties.getProperty( Constants.PACKAGE, configuration.getDefaultValue( Constants.PACKAGE ) ) );
227                 configuration.setDefaultProperty( Constants.PACKAGE, configuration.getProperty( Constants.PACKAGE ) );
228             }
229             else if( null != configuration.getProperty( Constants.GROUP_ID ) )
230             {
231                 configuration.setProperty( Constants.PACKAGE, configuration.getProperty( Constants.GROUP_ID ) );
232                 configuration.setDefaultProperty( Constants.PACKAGE, configuration.getProperty( Constants.PACKAGE ) );
233             }
234             getLogger().debug( "Setting property " + Constants.PACKAGE + "="
235                 + configuration.getProperty( Constants.PACKAGE ) );
236         }
237 
238         if( null != properties.getProperty( Constants.ARCHETYPE_POST_GENERATION_GOALS ) )
239         {
240             configuration.setProperty( Constants.ARCHETYPE_POST_GENERATION_GOALS,
241                 properties.getProperty( Constants.ARCHETYPE_POST_GENERATION_GOALS ) );
242         }
243 
244         return configuration;
245     }
246 
247     public ArchetypeConfiguration createArchetypeConfiguration( MavenProject project,
248         ArchetypeDefinition archetypeDefinition, Properties properties )
249     {
250         ArchetypeConfiguration configuration = new ArchetypeConfiguration();
251         getLogger().debug( "Creating ArchetypeConfiguration from ArchetypeDefinition, MavenProject and Properties" );
252 
253         configuration.setGroupId( properties.getProperty(Constants.ARCHETYPE_GROUP_ID) );
254         configuration.setArtifactId( properties.getProperty(Constants.ARCHETYPE_ARTIFACT_ID) );
255         configuration.setVersion( properties.getProperty(Constants.ARCHETYPE_VERSION) );
256 
257         Iterator requiredProperties = properties.keySet().iterator();
258 
259         while( requiredProperties.hasNext() )
260         {
261             String requiredProperty = (String) requiredProperties.next();
262 
263             if( requiredProperty.indexOf( "." ) < 0 )
264             {
265                 configuration.addRequiredProperty( requiredProperty );
266                 getLogger().debug( "Adding requiredProperty " + requiredProperty );
267                 configuration.setProperty( requiredProperty, properties.getProperty( requiredProperty ) );
268                 getLogger().debug( "Setting property " + requiredProperty + "="
269                     + configuration.getProperty( requiredProperty ) );
270             }
271         }
272 
273         configuration.addRequiredProperty( Constants.GROUP_ID );
274         getLogger().debug( "Adding requiredProperty " + Constants.GROUP_ID );
275         configuration.setDefaultProperty( Constants.GROUP_ID, project.getGroupId() );
276         if( null != properties.getProperty( Constants.GROUP_ID, null ) )
277         {
278             configuration.setProperty( Constants.GROUP_ID, properties.getProperty( Constants.GROUP_ID ) );
279             getLogger().debug( "Setting property " + Constants.GROUP_ID + "="
280                 + configuration.getProperty( Constants.GROUP_ID ) );
281         }
282 
283         configuration.addRequiredProperty( Constants.ARTIFACT_ID );
284         getLogger().debug( "Adding requiredProperty " + Constants.ARTIFACT_ID );
285         configuration.setDefaultProperty( Constants.ARTIFACT_ID, project.getArtifactId() );
286         if( null != properties.getProperty( Constants.ARTIFACT_ID, null ) )
287         {
288             configuration.setProperty( Constants.ARTIFACT_ID, properties.getProperty( Constants.ARTIFACT_ID ) );
289             getLogger().debug( "Setting property " + Constants.ARTIFACT_ID + "="
290                 + configuration.getProperty( Constants.ARTIFACT_ID ) );
291         }
292 
293         configuration.addRequiredProperty( Constants.VERSION );
294         getLogger().debug( "Adding requiredProperty " + Constants.VERSION );
295         configuration.setDefaultProperty( Constants.VERSION, project.getVersion() );
296         if( null != properties.getProperty( Constants.VERSION, null ) )
297         {
298             configuration.setProperty( Constants.VERSION, properties.getProperty( Constants.VERSION ) );
299             getLogger().debug( "Setting property " + Constants.VERSION + "="
300                 + configuration.getProperty( Constants.VERSION ) );
301         }
302 
303         configuration.addRequiredProperty( Constants.PACKAGE );
304         getLogger().debug( "Adding requiredProperty " + Constants.PACKAGE );
305         if( null != properties.getProperty( Constants.PACKAGE ) )
306         {
307             configuration.setProperty( Constants.PACKAGE, properties.getProperty( Constants.PACKAGE ) );
308 
309             getLogger().debug( "Setting property " + Constants.PACKAGE + "="
310                 + configuration.getProperty( Constants.PACKAGE ) );
311         }
312 
313         if( null != properties.getProperty( Constants.ARCHETYPE_GROUP_ID, null ) )
314         {
315             configuration.setProperty( Constants.ARCHETYPE_GROUP_ID, properties.getProperty( Constants.ARCHETYPE_GROUP_ID ) );
316         }
317 
318         if( null != properties.getProperty( Constants.ARCHETYPE_ARTIFACT_ID, null ) )
319         {
320             configuration.setProperty( Constants.ARCHETYPE_ARTIFACT_ID, properties.getProperty( Constants.ARCHETYPE_ARTIFACT_ID ) );
321         }
322 
323         if( null != properties.getProperty( Constants.ARCHETYPE_VERSION, null ) )
324         {
325             configuration.setProperty( Constants.ARCHETYPE_VERSION, properties.getProperty( Constants.ARCHETYPE_VERSION ) );
326         }
327         return configuration;
328     }
329 
330     public ArchetypeDefinition createArchetypeDefinition( Properties properties )
331     {
332         ArchetypeDefinition definition = new ArchetypeDefinition();
333 
334         definition.setGroupId( properties.getProperty( Constants.ARCHETYPE_GROUP_ID, null ) );
335 
336         definition.setArtifactId( properties.getProperty( Constants.ARCHETYPE_ARTIFACT_ID, null ) );
337 
338         definition.setVersion( properties.getProperty( Constants.ARCHETYPE_VERSION, null ) );
339 
340         definition.setRepository( properties.getProperty( Constants.ARCHETYPE_REPOSITORY, null ) );
341 
342         return definition;
343     }
344 
345     public void updateArchetypeConfiguration( ArchetypeConfiguration archetypeConfiguration,
346         ArchetypeDefinition archetypeDefinition )
347     {
348         archetypeConfiguration.setGroupId( archetypeDefinition.getGroupId() );
349         archetypeConfiguration.setArtifactId( archetypeDefinition.getArtifactId() );
350         archetypeConfiguration.setVersion( archetypeDefinition.getVersion() );
351     }
352 }