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