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