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.ArchetypeGenerationRequest;
23  import org.apache.maven.archetype.common.ArchetypeArtifactManager;
24  import org.apache.maven.archetype.common.ArchetypeConfiguration;
25  import org.apache.maven.archetype.common.ArchetypeDefinition;
26  import org.apache.maven.archetype.common.ArchetypeRegistryManager;
27  import org.apache.maven.archetype.common.Constants;
28  import org.apache.maven.archetype.exception.ArchetypeGenerationConfigurationFailure;
29  import org.apache.maven.archetype.exception.ArchetypeNotConfigured;
30  import org.apache.maven.archetype.exception.ArchetypeNotDefined;
31  import org.apache.maven.archetype.exception.UnknownArchetype;
32  import org.apache.maven.archetype.old.OldArchetype;
33  import org.apache.maven.artifact.repository.ArtifactRepository;
34  
35  import org.codehaus.plexus.components.interactivity.PrompterException;
36  import org.codehaus.plexus.logging.AbstractLogEnabled;
37  
38  import java.io.IOException;
39  
40  import java.util.ArrayList;
41  import java.util.Iterator;
42  import java.util.List;
43  import java.util.Properties;
44  
45  // TODO: this seems to have more responsibilities than just a configurator
46  /**
47   * @plexus.component
48   */
49  public class DefaultArchetypeGenerationConfigurator
50  extends AbstractLogEnabled
51  implements ArchetypeGenerationConfigurator
52  {
53      /**
54       * @plexus.requirement
55       */
56      OldArchetype oldArchetype;
57  
58      /**
59       * @plexus.requirement
60       */
61      private ArchetypeArtifactManager archetypeArtifactManager;
62  
63      /**
64       * @plexus.requirement
65       */
66      private ArchetypeFactory archetypeFactory;
67  
68      /**
69       * @plexus.requirement
70       */
71      private ArchetypeGenerationQueryer archetypeGenerationQueryer;
72  
73      /**
74       * @plexus.requirement
75       */
76      private ArchetypeRegistryManager archetypeRegistryManager;
77  
78      public void setArchetypeArtifactManager( ArchetypeArtifactManager archetypeArtifactManager )
79      {
80          this.archetypeArtifactManager = archetypeArtifactManager;
81      }
82  
83      public void configureArchetype( ArchetypeGenerationRequest request, Boolean interactiveMode,
84          Properties executionProperties )
85      throws ArchetypeNotDefined,
86          UnknownArchetype,
87          ArchetypeNotConfigured,
88          IOException,
89          PrompterException,
90          ArchetypeGenerationConfigurationFailure
91      {
92          ArtifactRepository localRepository = request.getLocalRepository();
93  
94          ArtifactRepository archetypeRepository = null;
95  
96          List repositories = new ArrayList();
97  
98          Properties properties = new Properties( executionProperties );
99  
100         ArchetypeDefinition ad = new ArchetypeDefinition();
101 
102         ad.setGroupId( request.getArchetypeGroupId() );
103 
104         ad.setArtifactId( request.getArchetypeArtifactId() );
105 
106         ad.setVersion( request.getArchetypeVersion() );
107 
108         if( !ad.isDefined() )
109         {
110             if( !interactiveMode.booleanValue() )
111             {
112                 throw new ArchetypeNotDefined( "No archetype was chosen" );
113             }
114             else
115             {
116                 throw new ArchetypeNotDefined( "The archetype is not defined" );
117             }
118         }
119         if( request.getArchetypeRepository() != null )
120         {
121             archetypeRepository = archetypeRegistryManager.createRepository( request.getArchetypeRepository(),
122                     ad.getArtifactId() + "-repo" );
123             repositories.add( archetypeRepository );
124         }
125         if( request.getRemoteArtifactRepositories() != null )
126         {
127             repositories.addAll( request.getRemoteArtifactRepositories() );
128         }
129 
130         if( !archetypeArtifactManager.exists( ad.getGroupId(), ad.getArtifactId(), ad.getVersion(), archetypeRepository,
131                 localRepository, repositories ) )
132         {
133             throw new UnknownArchetype( "The desired archetype does not exist (" + ad.getGroupId() + ":"
134                 + ad.getArtifactId() + ":" + ad.getVersion() + ")" );
135         }
136 
137         request.setArchetypeVersion( ad.getVersion() );
138 
139         ArchetypeConfiguration archetypeConfiguration;
140 
141         if( archetypeArtifactManager.isFileSetArchetype( ad.getGroupId(), ad.getArtifactId(), ad.getVersion(),
142                 archetypeRepository, localRepository, repositories ) )
143         {
144             org.apache.maven.archetype.metadata.ArchetypeDescriptor archetypeDescriptor = archetypeArtifactManager
145                 .getFileSetArchetypeDescriptor( ad.getGroupId(), ad.getArtifactId(), ad.getVersion(),
146                     archetypeRepository, localRepository, repositories );
147 
148             archetypeConfiguration = archetypeFactory.createArchetypeConfiguration( archetypeDescriptor, properties );
149         }
150         else if( archetypeArtifactManager.isOldArchetype( ad.getGroupId(), ad.getArtifactId(), ad.getVersion(),
151                 archetypeRepository, localRepository, repositories ) )
152         {
153             org.apache.maven.archetype.old.descriptor.ArchetypeDescriptor archetypeDescriptor = archetypeArtifactManager
154                 .getOldArchetypeDescriptor( ad.getGroupId(), ad.getArtifactId(), ad.getVersion(), archetypeRepository,
155                     localRepository, repositories );
156 
157             archetypeConfiguration = archetypeFactory.createArchetypeConfiguration( archetypeDescriptor, properties );
158         }
159         else
160         {
161             throw new ArchetypeGenerationConfigurationFailure( "The defined artifact is not an archetype" );
162         }
163 
164         if( interactiveMode.booleanValue() )
165         {
166             boolean confirmed = false;
167 
168             while( !confirmed )
169             {
170                 if( !archetypeConfiguration.isConfigured() )
171                 {
172                     Iterator requiredProperties = archetypeConfiguration.getRequiredProperties().iterator();
173 
174                     while( requiredProperties.hasNext() )
175                     {
176                         String requiredProperty = (String) requiredProperties.next();
177 
178                         if( !archetypeConfiguration.isConfigured( requiredProperty ) )
179                         {
180                             if( "package".equals(requiredProperty) ) {
181                                 // if the asked property is 'package', then
182                                 // use its default and if not defined,
183                                 // use the 'groupId' property value.
184                                 String packageDefault = archetypeConfiguration.getDefaultValue( requiredProperty );
185                                 packageDefault = (null == packageDefault || "".equals(packageDefault))?
186                                     archetypeConfiguration.getProperty( "groupId" ):
187                                     archetypeConfiguration.getDefaultValue( requiredProperty );
188                                 
189                                 archetypeConfiguration.setProperty( requiredProperty,
190                                     archetypeGenerationQueryer.getPropertyValue( requiredProperty,
191                                         packageDefault ) );
192                             } else {
193                                 archetypeConfiguration.setProperty( requiredProperty,
194                                     archetypeGenerationQueryer.getPropertyValue( requiredProperty,
195                                         archetypeConfiguration.getDefaultValue( requiredProperty ) ) );
196                             }
197                         }
198                     }
199                 }
200 
201                 if( !archetypeConfiguration.isConfigured() )
202                 {
203                     getLogger().warn( "Archetype is not fully configured" );
204                 }
205                 else if( !archetypeGenerationQueryer.confirmConfiguration( archetypeConfiguration ) )
206                 {
207                     getLogger().debug( "Archetype generation configuration not confirmed" );
208                     archetypeConfiguration.reset();
209                     restoreCommandLineProperties( archetypeConfiguration, executionProperties );
210                 }
211                 else
212                 {
213                     getLogger().debug( "Archetype generation configuration confirmed" );
214 
215                     confirmed = true;
216                 }
217             }
218         }
219         else
220         {
221             if( !archetypeConfiguration.isConfigured() )
222             {
223                 Iterator requiredProperties = archetypeConfiguration.getRequiredProperties().iterator();
224 
225                 while( requiredProperties.hasNext() )
226                 {
227                     String requiredProperty = (String) requiredProperties.next();
228 
229                     if( !archetypeConfiguration.isConfigured( requiredProperty )
230                         && ( archetypeConfiguration.getDefaultValue( requiredProperty ) != null ) )
231                     {
232                         archetypeConfiguration.setProperty( requiredProperty,
233                             archetypeConfiguration.getDefaultValue( requiredProperty ) );
234                     }
235                 }
236 
237                 // in batch mode, we assume the defaults, and if still not configured fail
238                 if( !archetypeConfiguration.isConfigured() )
239                 {
240                     StringBuffer exceptionMessage = new StringBuffer();
241                     exceptionMessage.append("Archetype " );
242                     exceptionMessage.append( request.getArchetypeGroupId() );
243                     exceptionMessage.append( ":" );
244                     exceptionMessage.append( request.getArchetypeArtifactId() );
245                     exceptionMessage.append( ":" );
246                     exceptionMessage.append( request.getArchetypeVersion() );
247                     exceptionMessage.append( " is not configured" );
248                     
249                     List missingProperties = new ArrayList( 0 );
250                     requiredProperties = archetypeConfiguration.getRequiredProperties().iterator();
251                     while( requiredProperties.hasNext() )
252                     {
253                         String requiredProperty = (String)requiredProperties.next();
254                         if (!archetypeConfiguration.isConfigured( requiredProperty ))
255                         {
256                             exceptionMessage.append( "\n\tProperty " );
257                             exceptionMessage.append( requiredProperty );
258                             missingProperties.add( requiredProperty );
259                             exceptionMessage.append( " is missing." );
260                             getLogger().warn( "Property " + requiredProperty + 
261                                 " is missing. Add -D" + requiredProperty + "=someValue" );
262                         }
263                     }
264                     
265                     throw new ArchetypeNotConfigured( exceptionMessage.toString(), missingProperties );
266                 }
267             }
268         }
269 
270         request.setGroupId( archetypeConfiguration.getProperty( Constants.GROUP_ID ) );
271 
272         request.setArtifactId( archetypeConfiguration.getProperty( Constants.ARTIFACT_ID ) );
273 
274         request.setVersion( archetypeConfiguration.getProperty( Constants.VERSION ) );
275 
276         request.setPackage( archetypeConfiguration.getProperty( Constants.PACKAGE ) );
277 
278         properties = archetypeConfiguration.getProperties();
279 
280         request.setProperties( properties );
281     }
282 
283     private void restoreCommandLineProperties( ArchetypeConfiguration archetypeConfiguration,
284         Properties executionProperties )
285     {
286         getLogger().debug( "Restoring command line properties" );
287 
288         Iterator properties = archetypeConfiguration.getRequiredProperties().iterator();
289         while( properties.hasNext() )
290         {
291             String property = (String) properties.next();
292             if( executionProperties.containsKey( property ) )
293             {
294                 archetypeConfiguration.setProperty( property, executionProperties.getProperty( property ) );
295                 getLogger().debug( "Restored " + property + "=" + archetypeConfiguration.getProperty( property ) );
296             }
297         }
298     }
299 }