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                             archetypeConfiguration.setProperty( requiredProperty,
181                                 archetypeGenerationQueryer.getPropertyValue( requiredProperty,
182                                     archetypeConfiguration.getDefaultValue( requiredProperty ) ) );
183                         }
184                     }
185                 }
186 
187                 if( !archetypeConfiguration.isConfigured() )
188                 {
189                     throw new ArchetypeGenerationConfigurationFailure(
190                         "The archetype generation must be configured here" );
191                 }
192                 else if( !archetypeGenerationQueryer.confirmConfiguration( archetypeConfiguration ) )
193                 {
194                     getLogger().debug( "Archetype generation configuration not confirmed" );
195                     archetypeConfiguration.reset();
196                     restoreCommandLineProperties( archetypeConfiguration, executionProperties );
197                 }
198                 else
199                 {
200                     getLogger().debug( "Archetype generation configuration confirmed" );
201 
202                     confirmed = true;
203                 }
204             }
205         }
206         else
207         {
208             if( !archetypeConfiguration.isConfigured() )
209             {
210                 Iterator requiredProperties = archetypeConfiguration.getRequiredProperties().iterator();
211 
212                 while( requiredProperties.hasNext() )
213                 {
214                     String requiredProperty = (String) requiredProperties.next();
215 
216                     if( !archetypeConfiguration.isConfigured( requiredProperty )
217                         && ( archetypeConfiguration.getDefaultValue( requiredProperty ) != null ) )
218                     {
219                         archetypeConfiguration.setProperty( requiredProperty,
220                             archetypeConfiguration.getDefaultValue( requiredProperty ) );
221                     }
222                 }
223 
224                 // in batch mode, we assume the defaults, and if still not configured fail
225                 if( !archetypeConfiguration.isConfigured() )
226                 {
227                     throw new ArchetypeNotConfigured( "Archetype " + request.getArchetypeGroupId() + ":"
228                         + request.getArchetypeArtifactId() + ":" + request.getArchetypeVersion()
229                         + " is not configured" );
230                 }
231             }
232         } // end if-else
233 
234         request.setGroupId( archetypeConfiguration.getProperty( Constants.GROUP_ID ) );
235 
236         request.setArtifactId( archetypeConfiguration.getProperty( Constants.ARTIFACT_ID ) );
237 
238         request.setVersion( archetypeConfiguration.getProperty( Constants.VERSION ) );
239 
240         request.setPackage( archetypeConfiguration.getProperty( Constants.PACKAGE ) );
241 
242         properties = archetypeConfiguration.getProperties();
243 
244         request.setProperties( properties );
245     }
246 
247     private void restoreCommandLineProperties( ArchetypeConfiguration archetypeConfiguration,
248         Properties executionProperties )
249     {
250         getLogger().debug( "Restoring command line properties" );
251 
252         Iterator properties = archetypeConfiguration.getRequiredProperties().iterator();
253         while( properties.hasNext() )
254         {
255             String property = (String) properties.next();
256             if( executionProperties.containsKey( property ) )
257             {
258                 archetypeConfiguration.setProperty( property, executionProperties.getProperty( property ) );
259                 getLogger().debug( "Restored " + property + "=" + archetypeConfiguration.getProperty( property ) );
260             }
261         }
262     }
263 }