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