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.common.Constants;
23 import org.apache.maven.archetype.metadata.RequiredProperty;
24 import org.apache.maven.project.MavenProject;
25 import org.codehaus.plexus.component.annotations.Component;
26 import org.codehaus.plexus.logging.AbstractLogEnabled;
27
28 import java.util.Iterator;
29 import java.util.Properties;
30
31 @Component( role = ArchetypeFactory.class, hint = "default" )
32 public class DefaultArchetypeFactory
33 extends AbstractLogEnabled
34 implements ArchetypeFactory
35 {
36 @Override
37 public ArchetypeDefinition createArchetypeDefinition( Properties properties )
38 {
39 ArchetypeDefinition definition = new ArchetypeDefinition();
40
41 definition.setGroupId( properties.getProperty( Constants.ARCHETYPE_GROUP_ID ) );
42
43 definition.setArtifactId( properties.getProperty( Constants.ARCHETYPE_ARTIFACT_ID ) );
44
45 definition.setVersion( properties.getProperty( Constants.ARCHETYPE_VERSION ) );
46
47 definition.setRepository( properties.getProperty( Constants.ARCHETYPE_REPOSITORY ) );
48
49 definition.setUrl( properties.getProperty( Constants.ARCHETYPE_URL ) );
50
51 definition.setDescription( properties.getProperty( Constants.ARCHETYPE_DESCRIPTION ) );
52
53 return definition;
54 }
55
56 private void addOldRequiredProperty( ArchetypeConfiguration configuration, Properties properties, String key,
57 String defaultValue, boolean initPropertyWithDefault )
58 {
59 getLogger().debug( "Adding requiredProperty " + key );
60
61 configuration.addRequiredProperty( key );
62
63 String property = properties.getProperty( key );
64
65 if ( property != null )
66 {
67 configuration.setProperty( key, property );
68 configuration.setDefaultProperty( key, property );
69 }
70 else if ( defaultValue != null )
71 {
72 if ( initPropertyWithDefault )
73 {
74 configuration.setProperty( key, defaultValue );
75 }
76 configuration.setDefaultProperty( key, defaultValue );
77 }
78
79 getLogger().debug( "Setting property " + key + "=" + configuration.getProperty( key ) );
80 }
81
82 @Override
83 @SuppressWarnings( "checkstyle:linelength" )
84 public ArchetypeConfiguration createArchetypeConfiguration( org.apache.maven.archetype.old.descriptor.ArchetypeDescriptor archetypeDescriptor,
85 Properties properties )
86 {
87 getLogger().debug( "Creating ArchetypeConfiguration from legacy descriptor and Properties" );
88
89 ArchetypeConfiguration configuration = createArchetypeConfiguration( properties );
90
91 configuration.setName( archetypeDescriptor.getId() );
92
93 addOldRequiredProperty( configuration, properties, Constants.GROUP_ID, null, false );
94
95 addOldRequiredProperty( configuration, properties, Constants.ARTIFACT_ID, null, false );
96
97 addOldRequiredProperty( configuration, properties, Constants.VERSION, "1.0-SNAPSHOT", false );
98
99 addOldRequiredProperty( configuration, properties, Constants.PACKAGE,
100 configuration.getProperty( Constants.GROUP_ID ), true );
101
102 return configuration;
103 }
104
105 private void addRequiredProperty( ArchetypeConfiguration configuration, Properties properties, String key,
106 String defaultValue, boolean initPropertyWithDefault )
107 {
108 if ( !configuration.isConfigured( key ) && configuration.getDefaultValue( key ) == null )
109 {
110 addOldRequiredProperty( configuration, properties, key, defaultValue, initPropertyWithDefault );
111 }
112 }
113
114 @Override
115 @SuppressWarnings( "checkstyle:linelength" )
116 public ArchetypeConfiguration createArchetypeConfiguration( org.apache.maven.archetype.metadata.ArchetypeDescriptor archetypeDescriptor,
117 Properties properties )
118 {
119 getLogger().debug( "Creating ArchetypeConfiguration from fileset descriptor and Properties" );
120
121 ArchetypeConfiguration configuration = createArchetypeConfiguration( properties );
122
123 configuration.setName( archetypeDescriptor.getName() );
124
125 for ( RequiredProperty requiredProperty : archetypeDescriptor.getRequiredProperties() )
126 {
127 String key = requiredProperty.getKey();
128 getLogger().debug( "Adding requiredProperty " + key );
129
130 configuration.addRequiredProperty( key );
131
132 String defaultValue = requiredProperty.getDefaultValue();
133 String validationRegex = requiredProperty.getValidationRegex();
134
135 if ( properties.getProperty( key ) != null )
136 {
137
138 String value = properties.getProperty( key );
139 configuration.setProperty( key, value );
140 getLogger().debug( "Setting property " + key + "=" + value );
141 }
142 else if ( ( defaultValue != null ) && !containsInnerProperty( defaultValue ) )
143 {
144
145 configuration.setProperty( key, defaultValue );
146 getLogger().debug( "Setting property " + key + "=" + defaultValue );
147 }
148
149 if ( defaultValue != null )
150 {
151 configuration.setDefaultProperty( key, defaultValue );
152 getLogger().debug( "Setting defaultProperty " + key + "=" + defaultValue );
153 }
154
155 if ( validationRegex != null )
156 {
157 configuration.setPropertyValidationRegex( key, validationRegex );
158 getLogger().debug( "Setting validation regular expression " + key + "=" + defaultValue );
159 }
160 }
161
162 addRequiredProperty( configuration, properties, Constants.GROUP_ID, null, false );
163
164 addRequiredProperty( configuration, properties, Constants.ARTIFACT_ID, null, false );
165
166 addRequiredProperty( configuration, properties, Constants.VERSION, "1.0-SNAPSHOT", false );
167
168 addRequiredProperty( configuration, properties, Constants.PACKAGE,
169 configuration.getProperty( Constants.GROUP_ID ), true );
170
171 String postGenerationGoals = properties.getProperty( Constants.ARCHETYPE_POST_GENERATION_GOALS );
172 if ( postGenerationGoals != null )
173 {
174 configuration.setProperty( Constants.ARCHETYPE_POST_GENERATION_GOALS, postGenerationGoals );
175 }
176
177 return configuration;
178 }
179
180 private void addRequiredProperty( ArchetypeConfiguration configuration, Properties properties, String key,
181 String defaultValue )
182 {
183 getLogger().debug( "Adding requiredProperty " + key );
184
185 configuration.addRequiredProperty( key );
186
187 if ( defaultValue != null )
188 {
189 configuration.setDefaultProperty( key, defaultValue );
190 }
191
192 if ( properties.getProperty( key ) != null )
193 {
194 configuration.setProperty( key, properties.getProperty( key ) );
195
196 getLogger().debug( "Setting property " + key + "=" + configuration.getProperty( Constants.GROUP_ID ) );
197 }
198 }
199
200 private void setProperty( ArchetypeConfiguration configuration, Properties properties, String key )
201 {
202 String property = properties.getProperty( key );
203
204 if ( property != null )
205 {
206 configuration.setProperty( key, property );
207 }
208 }
209
210 @Override
211 public ArchetypeConfiguration createArchetypeConfiguration( MavenProject project,
212 ArchetypeDefinition archetypeDefinition,
213 Properties properties )
214 {
215 getLogger().debug( "Creating ArchetypeConfiguration from ArchetypeDefinition, MavenProject and Properties" );
216
217 ArchetypeConfiguration configuration = createArchetypeConfiguration( properties );
218
219 for ( Iterator<?> requiredProperties = properties.keySet().iterator(); requiredProperties.hasNext(); )
220 {
221 String requiredProperty = (String) requiredProperties.next();
222
223 if ( !requiredProperty.contains( "." ) )
224 {
225 getLogger().debug( "Adding requiredProperty " + requiredProperty );
226 configuration.addRequiredProperty( requiredProperty );
227
228 configuration.setProperty( requiredProperty, properties.getProperty( requiredProperty ) );
229 getLogger().debug( "Setting property " + requiredProperty + "="
230 + configuration.getProperty( requiredProperty ) );
231 }
232 }
233
234 addRequiredProperty( configuration, properties, Constants.GROUP_ID, project.getGroupId() );
235
236 addRequiredProperty( configuration, properties, Constants.ARTIFACT_ID, project.getArtifactId() );
237
238 addRequiredProperty( configuration, properties, Constants.VERSION, project.getVersion() );
239
240 addRequiredProperty( configuration, properties, Constants.PACKAGE, null );
241
242 setProperty( configuration, properties, Constants.ARCHETYPE_GROUP_ID );
243
244 setProperty( configuration, properties, Constants.ARCHETYPE_ARTIFACT_ID );
245
246 setProperty( configuration, properties, Constants.ARCHETYPE_VERSION );
247
248 setProperty( configuration, properties, Constants.ARCHETYPE_URL );
249
250 setProperty( configuration, properties, Constants.ARCHETYPE_DESCRIPTION );
251
252 return configuration;
253 }
254
255 private ArchetypeConfiguration createArchetypeConfiguration( Properties properties )
256 {
257 ArchetypeConfiguration configuration = new ArchetypeConfiguration();
258
259 configuration.setGroupId( properties.getProperty( Constants.ARCHETYPE_GROUP_ID ) );
260
261 configuration.setArtifactId( properties.getProperty( Constants.ARCHETYPE_ARTIFACT_ID ) );
262
263 configuration.setVersion( properties.getProperty( Constants.ARCHETYPE_VERSION ) );
264
265 configuration.setUrl( properties.getProperty( Constants.ARCHETYPE_URL ) );
266
267 configuration.setDescription( properties.getProperty( Constants.ARCHETYPE_DESCRIPTION ) );
268
269 return configuration;
270 }
271
272 @Override
273 public void updateArchetypeConfiguration( ArchetypeConfiguration archetypeConfiguration,
274 ArchetypeDefinition archetypeDefinition )
275 {
276 archetypeConfiguration.setGroupId( archetypeDefinition.getGroupId() );
277 archetypeConfiguration.setArtifactId( archetypeDefinition.getArtifactId() );
278 archetypeConfiguration.setVersion( archetypeDefinition.getVersion() );
279 }
280
281
282
283
284
285
286
287 private boolean containsInnerProperty( String defaultValue )
288 {
289 if ( defaultValue == null )
290 {
291 return false;
292 }
293 int start = defaultValue.indexOf( "${" );
294 return ( start >= 0 ) && ( defaultValue.indexOf( "}", start ) >= 0 );
295 }
296 }