1 | |
package org.apache.maven.archetype.ui; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import org.apache.maven.archetype.ArchetypeGenerationRequest; |
23 | |
import org.apache.maven.archetype.common.ArchetypeArtifactManager; |
24 | |
import org.apache.maven.archetype.common.ArchetypeRegistryManager; |
25 | |
import org.apache.maven.archetype.common.Constants; |
26 | |
import org.apache.maven.archetype.exception.ArchetypeGenerationConfigurationFailure; |
27 | |
import org.apache.maven.archetype.exception.ArchetypeNotConfigured; |
28 | |
import org.apache.maven.archetype.exception.ArchetypeNotDefined; |
29 | |
import org.apache.maven.archetype.exception.UnknownArchetype; |
30 | |
import org.apache.maven.archetype.old.OldArchetype; |
31 | |
import org.apache.maven.artifact.repository.ArtifactRepository; |
32 | |
|
33 | |
import org.codehaus.plexus.components.interactivity.PrompterException; |
34 | |
import org.codehaus.plexus.logging.AbstractLogEnabled; |
35 | |
import org.codehaus.plexus.util.StringUtils; |
36 | |
|
37 | |
import java.io.IOException; |
38 | |
|
39 | |
import java.util.ArrayList; |
40 | |
import java.util.Collections; |
41 | |
import java.util.Comparator; |
42 | |
import java.util.List; |
43 | |
import java.util.Properties; |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | 3 | public class DefaultArchetypeGenerationConfigurator |
50 | |
extends AbstractLogEnabled |
51 | |
implements ArchetypeGenerationConfigurator |
52 | |
{ |
53 | |
|
54 | |
|
55 | |
|
56 | |
OldArchetype oldArchetype; |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
private ArchetypeArtifactManager archetypeArtifactManager; |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
private ArchetypeFactory archetypeFactory; |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
private ArchetypeGenerationQueryer archetypeGenerationQueryer; |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
private ArchetypeRegistryManager archetypeRegistryManager; |
77 | |
|
78 | |
public void setArchetypeArtifactManager( ArchetypeArtifactManager archetypeArtifactManager ) |
79 | |
{ |
80 | 3 | this.archetypeArtifactManager = archetypeArtifactManager; |
81 | 3 | } |
82 | |
|
83 | |
public void configureArchetype( ArchetypeGenerationRequest request, Boolean interactiveMode, |
84 | |
Properties executionProperties ) |
85 | |
throws ArchetypeNotDefined, UnknownArchetype, ArchetypeNotConfigured, IOException, PrompterException, |
86 | |
ArchetypeGenerationConfigurationFailure |
87 | |
{ |
88 | 3 | ArtifactRepository localRepository = request.getLocalRepository(); |
89 | |
|
90 | 3 | ArtifactRepository archetypeRepository = null; |
91 | |
|
92 | 3 | List<ArtifactRepository> repositories = new ArrayList<ArtifactRepository>(); |
93 | |
|
94 | 3 | Properties properties = new Properties( executionProperties ); |
95 | |
|
96 | 3 | ArchetypeDefinition ad = new ArchetypeDefinition( request ); |
97 | |
|
98 | 3 | if ( !ad.isDefined() ) |
99 | |
{ |
100 | 0 | if ( !interactiveMode.booleanValue() ) |
101 | |
{ |
102 | 0 | throw new ArchetypeNotDefined( "No archetype was chosen" ); |
103 | |
} |
104 | |
else |
105 | |
{ |
106 | 0 | throw new ArchetypeNotDefined( "The archetype is not defined" ); |
107 | |
} |
108 | |
} |
109 | 3 | if ( request.getArchetypeRepository() != null ) |
110 | |
{ |
111 | 0 | archetypeRepository = |
112 | |
archetypeRegistryManager.createRepository( request.getArchetypeRepository(), |
113 | |
ad.getArtifactId() + "-repo" ); |
114 | 0 | repositories.add( archetypeRepository ); |
115 | |
} |
116 | 3 | if ( request.getRemoteArtifactRepositories() != null ) |
117 | |
{ |
118 | 0 | repositories.addAll( request.getRemoteArtifactRepositories() ); |
119 | |
} |
120 | |
|
121 | 3 | if ( !archetypeArtifactManager.exists( ad.getGroupId(), ad.getArtifactId(), ad.getVersion(), |
122 | |
archetypeRepository, localRepository, repositories ) ) |
123 | |
{ |
124 | 0 | throw new UnknownArchetype( "The desired archetype does not exist (" + ad.getGroupId() + ":" |
125 | |
+ ad.getArtifactId() + ":" + ad.getVersion() + ")" ); |
126 | |
} |
127 | |
|
128 | 3 | request.setArchetypeVersion( ad.getVersion() ); |
129 | |
|
130 | |
ArchetypeConfiguration archetypeConfiguration; |
131 | |
|
132 | 3 | if ( archetypeArtifactManager.isFileSetArchetype( ad.getGroupId(), ad.getArtifactId(), ad.getVersion(), |
133 | |
archetypeRepository, localRepository, repositories ) ) |
134 | |
{ |
135 | 0 | org.apache.maven.archetype.metadata.ArchetypeDescriptor archetypeDescriptor = |
136 | |
archetypeArtifactManager.getFileSetArchetypeDescriptor( ad.getGroupId(), ad.getArtifactId(), |
137 | |
ad.getVersion(), archetypeRepository, |
138 | |
localRepository, repositories ); |
139 | |
|
140 | 0 | archetypeConfiguration = archetypeFactory.createArchetypeConfiguration( archetypeDescriptor, properties ); |
141 | 0 | } |
142 | 3 | else if ( archetypeArtifactManager.isOldArchetype( ad.getGroupId(), ad.getArtifactId(), ad.getVersion(), |
143 | |
archetypeRepository, localRepository, repositories ) ) |
144 | |
{ |
145 | 3 | org.apache.maven.archetype.old.descriptor.ArchetypeDescriptor archetypeDescriptor = |
146 | |
archetypeArtifactManager.getOldArchetypeDescriptor( ad.getGroupId(), ad.getArtifactId(), |
147 | |
ad.getVersion(), archetypeRepository, |
148 | |
localRepository, repositories ); |
149 | |
|
150 | 3 | archetypeConfiguration = archetypeFactory.createArchetypeConfiguration( archetypeDescriptor, properties ); |
151 | 3 | } |
152 | |
else |
153 | |
{ |
154 | 0 | throw new ArchetypeGenerationConfigurationFailure( "The defined artifact is not an archetype" ); |
155 | |
} |
156 | |
|
157 | 3 | if ( interactiveMode.booleanValue() ) |
158 | |
{ |
159 | 0 | boolean confirmed = false; |
160 | |
|
161 | 0 | while ( !confirmed ) |
162 | |
{ |
163 | 0 | List<String> propertiesRequired = archetypeConfiguration.getRequiredProperties(); |
164 | 0 | getLogger().debug( "Required properties before content sort: " + propertiesRequired ); |
165 | 0 | Collections.sort( propertiesRequired, new RequiredPropertyComparator( archetypeConfiguration ) ); |
166 | 0 | getLogger().debug( "Required properties after content sort: " + propertiesRequired ); |
167 | |
|
168 | 0 | if ( !archetypeConfiguration.isConfigured() ) |
169 | |
{ |
170 | 0 | for ( String requiredProperty : propertiesRequired ) |
171 | |
{ |
172 | 0 | if ( !archetypeConfiguration.isConfigured( requiredProperty ) ) |
173 | |
{ |
174 | 0 | if ( "package".equals( requiredProperty ) ) |
175 | |
{ |
176 | |
|
177 | |
|
178 | |
|
179 | 0 | String packageDefault = archetypeConfiguration.getDefaultValue( requiredProperty ); |
180 | 0 | packageDefault = |
181 | |
( null == packageDefault || "".equals( packageDefault ) ) ? archetypeConfiguration.getProperty( "groupId" ) |
182 | |
: archetypeConfiguration.getDefaultValue( requiredProperty ); |
183 | |
|
184 | 0 | String value = getTransitiveDefaultValue( packageDefault, archetypeConfiguration ); |
185 | |
|
186 | 0 | value = archetypeGenerationQueryer.getPropertyValue( requiredProperty, value ); |
187 | |
|
188 | 0 | archetypeConfiguration.setProperty( requiredProperty, value ); |
189 | 0 | } |
190 | |
else |
191 | |
{ |
192 | 0 | String value = archetypeConfiguration.getDefaultValue( requiredProperty ); |
193 | |
|
194 | 0 | value = getTransitiveDefaultValue( value, archetypeConfiguration ); |
195 | |
|
196 | 0 | value = archetypeGenerationQueryer.getPropertyValue( requiredProperty, value ); |
197 | |
|
198 | 0 | archetypeConfiguration.setProperty( requiredProperty, value ); |
199 | 0 | } |
200 | |
} |
201 | |
else |
202 | |
{ |
203 | 0 | getLogger().info( |
204 | |
"Using property: " + requiredProperty + " = " |
205 | |
+ archetypeConfiguration.getProperty( requiredProperty ) ); |
206 | |
} |
207 | |
} |
208 | |
} |
209 | |
else |
210 | |
{ |
211 | |
|
212 | 0 | for ( String requiredProperty : propertiesRequired ) |
213 | |
{ |
214 | 0 | getLogger().info( |
215 | |
"Using property: " + requiredProperty + " = " |
216 | |
+ archetypeConfiguration.getProperty( requiredProperty ) ); |
217 | |
} |
218 | |
} |
219 | |
|
220 | 0 | if ( !archetypeConfiguration.isConfigured() ) |
221 | |
{ |
222 | 0 | getLogger().warn( "Archetype is not fully configured" ); |
223 | |
} |
224 | 0 | else if ( !archetypeGenerationQueryer.confirmConfiguration( archetypeConfiguration ) ) |
225 | |
{ |
226 | 0 | getLogger().debug( "Archetype generation configuration not confirmed" ); |
227 | 0 | archetypeConfiguration.reset(); |
228 | 0 | restoreCommandLineProperties( archetypeConfiguration, executionProperties ); |
229 | |
} |
230 | |
else |
231 | |
{ |
232 | 0 | getLogger().debug( "Archetype generation configuration confirmed" ); |
233 | |
|
234 | 0 | confirmed = true; |
235 | |
} |
236 | 0 | } |
237 | 0 | } |
238 | |
else |
239 | |
{ |
240 | 3 | if ( !archetypeConfiguration.isConfigured() ) |
241 | |
{ |
242 | 2 | for ( String requiredProperty : archetypeConfiguration.getRequiredProperties() ) |
243 | |
{ |
244 | 8 | if ( !archetypeConfiguration.isConfigured( requiredProperty ) |
245 | |
&& ( archetypeConfiguration.getDefaultValue( requiredProperty ) != null ) ) |
246 | |
{ |
247 | 2 | archetypeConfiguration.setProperty( requiredProperty, |
248 | |
archetypeConfiguration.getDefaultValue( requiredProperty ) ); |
249 | |
} |
250 | |
} |
251 | |
|
252 | |
|
253 | 2 | if ( !archetypeConfiguration.isConfigured() ) |
254 | |
{ |
255 | 1 | StringBuffer exceptionMessage = new StringBuffer(); |
256 | 1 | exceptionMessage.append( "Archetype " ); |
257 | 1 | exceptionMessage.append( request.getArchetypeGroupId() ); |
258 | 1 | exceptionMessage.append( ":" ); |
259 | 1 | exceptionMessage.append( request.getArchetypeArtifactId() ); |
260 | 1 | exceptionMessage.append( ":" ); |
261 | 1 | exceptionMessage.append( request.getArchetypeVersion() ); |
262 | 1 | exceptionMessage.append( " is not configured" ); |
263 | |
|
264 | 1 | List<String> missingProperties = new ArrayList<String>( 0 ); |
265 | 1 | for ( String requiredProperty : archetypeConfiguration.getRequiredProperties() ) |
266 | |
{ |
267 | 4 | if ( !archetypeConfiguration.isConfigured( requiredProperty ) ) |
268 | |
{ |
269 | 2 | exceptionMessage.append( "\n\tProperty " ); |
270 | 2 | exceptionMessage.append( requiredProperty ); |
271 | 2 | missingProperties.add( requiredProperty ); |
272 | 2 | exceptionMessage.append( " is missing." ); |
273 | 2 | getLogger().warn( |
274 | |
"Property " + requiredProperty + " is missing. Add -D" + requiredProperty |
275 | |
+ "=someValue" ); |
276 | |
} |
277 | |
} |
278 | |
|
279 | 1 | throw new ArchetypeNotConfigured( exceptionMessage.toString(), missingProperties ); |
280 | |
} |
281 | |
} |
282 | |
} |
283 | |
|
284 | 2 | request.setGroupId( archetypeConfiguration.getProperty( Constants.GROUP_ID ) ); |
285 | |
|
286 | 2 | request.setArtifactId( archetypeConfiguration.getProperty( Constants.ARTIFACT_ID ) ); |
287 | |
|
288 | 2 | request.setVersion( archetypeConfiguration.getProperty( Constants.VERSION ) ); |
289 | |
|
290 | 2 | request.setPackage( archetypeConfiguration.getProperty( Constants.PACKAGE ) ); |
291 | |
|
292 | 2 | properties = archetypeConfiguration.getProperties(); |
293 | |
|
294 | 2 | request.setProperties( properties ); |
295 | 2 | } |
296 | |
|
297 | |
private String getTransitiveDefaultValue( String defaultValue, ArchetypeConfiguration archetypeConfiguration ) |
298 | |
{ |
299 | 0 | String result = defaultValue; |
300 | 0 | if ( null == result ) |
301 | |
{ |
302 | 0 | return null; |
303 | |
} |
304 | 0 | for ( String property : archetypeConfiguration.getRequiredProperties() ) |
305 | |
{ |
306 | 0 | if ( result.indexOf( "${" + property + "}" ) >= 0 ) |
307 | |
{ |
308 | 0 | result = StringUtils.replace( result, "${" + property + "}", |
309 | |
archetypeConfiguration.getProperty( property ) ); |
310 | |
} |
311 | |
} |
312 | 0 | return result; |
313 | |
} |
314 | |
|
315 | |
private void restoreCommandLineProperties( ArchetypeConfiguration archetypeConfiguration, |
316 | |
Properties executionProperties ) |
317 | |
{ |
318 | 0 | getLogger().debug( "Restoring command line properties" ); |
319 | |
|
320 | 0 | for ( String property : archetypeConfiguration.getRequiredProperties() ) |
321 | |
{ |
322 | 0 | if ( executionProperties.containsKey( property ) ) |
323 | |
{ |
324 | 0 | archetypeConfiguration.setProperty( property, executionProperties.getProperty( property ) ); |
325 | 0 | getLogger().debug( "Restored " + property + "=" + archetypeConfiguration.getProperty( property ) ); |
326 | |
} |
327 | |
} |
328 | 0 | } |
329 | |
|
330 | 0 | public static class RequiredPropertyComparator implements Comparator<String> |
331 | |
{ |
332 | |
private final ArchetypeConfiguration archetypeConfiguration; |
333 | |
|
334 | |
public RequiredPropertyComparator( ArchetypeConfiguration archetypeConfiguration ) |
335 | 0 | { |
336 | 0 | this.archetypeConfiguration = archetypeConfiguration; |
337 | 0 | } |
338 | |
|
339 | |
public int compare( String left, String right ) |
340 | |
{ |
341 | 0 | String leftDefault = archetypeConfiguration.getDefaultValue( left ); |
342 | 0 | String rightDefault = archetypeConfiguration.getDefaultValue( right ); |
343 | 0 | if ( null == leftDefault || null == rightDefault ) |
344 | |
{ |
345 | 0 | return comparePropertyName( (String) left, (String) right ); |
346 | |
} |
347 | 0 | else if ( leftDefault.indexOf( "${" + right + "}" ) >= 0 ) |
348 | |
{ |
349 | 0 | return 1; |
350 | |
} |
351 | 0 | else if ( rightDefault.indexOf( "${" + left + "}" ) >= 0 ) |
352 | |
{ |
353 | 0 | return -1; |
354 | |
} |
355 | |
else |
356 | |
{ |
357 | 0 | return comparePropertyName( left, right ); |
358 | |
} |
359 | |
} |
360 | |
|
361 | |
private int comparePropertyName( String left, String right ) |
362 | |
{ |
363 | 0 | if ( "groupId".equals( left ) ) |
364 | |
{ |
365 | 0 | return -1; |
366 | |
} |
367 | 0 | if ( "groupId".equals( right ) ) |
368 | |
{ |
369 | 0 | return 1; |
370 | |
} |
371 | 0 | if ( "artifactId".equals( left ) ) |
372 | |
{ |
373 | 0 | return -1; |
374 | |
} |
375 | 0 | if ( "artifactId".equals( right ) ) |
376 | |
{ |
377 | 0 | return 1; |
378 | |
} |
379 | 0 | if ( "version".equals( left ) ) |
380 | |
{ |
381 | 0 | return -1; |
382 | |
} |
383 | 0 | if ( "version".equals( right ) ) |
384 | |
{ |
385 | 0 | return 1; |
386 | |
} |
387 | 0 | if ( "package".equals( left ) ) |
388 | |
{ |
389 | 0 | return -1; |
390 | |
} |
391 | 0 | if ( "package".equals( right ) ) |
392 | |
{ |
393 | 0 | return 1; |
394 | |
} |
395 | 0 | return left.compareTo( right ); |
396 | |
} |
397 | |
} |
398 | |
} |