1 package org.apache.maven.archetype.mojos;
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.ArchetypeCreationRequest;
23 import org.apache.maven.archetype.ArchetypeCreationResult;
24 import org.apache.maven.archetype.ArchetypeManager;
25 import org.apache.maven.archetype.common.Constants;
26 import org.apache.maven.archetype.ui.creation.ArchetypeCreationConfigurator;
27 import org.apache.maven.artifact.repository.ArtifactRepository;
28 import org.apache.maven.execution.MavenSession;
29 import org.apache.maven.plugin.AbstractMojo;
30 import org.apache.maven.plugin.MojoExecutionException;
31 import org.apache.maven.plugin.MojoFailureException;
32 import org.apache.maven.plugins.annotations.Component;
33 import org.apache.maven.plugins.annotations.Execute;
34 import org.apache.maven.plugins.annotations.LifecyclePhase;
35 import org.apache.maven.plugins.annotations.Mojo;
36 import org.apache.maven.plugins.annotations.Parameter;
37 import org.apache.maven.project.MavenProject;
38 import org.codehaus.plexus.util.PropertyUtils;
39 import org.codehaus.plexus.util.StringUtils;
40
41 import java.io.File;
42 import java.util.ArrayList;
43 import java.util.Arrays;
44 import java.util.List;
45 import java.util.Properties;
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92 @Mojo( name = "create-from-project", requiresProject = true, aggregator = true )
93 @Execute( phase = LifecyclePhase.GENERATE_SOURCES )
94 public class CreateArchetypeFromProjectMojo
95 extends AbstractMojo
96 {
97
98 @Component
99 private ArchetypeCreationConfigurator configurator;
100
101
102
103
104 @Parameter( property = "interactive", defaultValue = "false" )
105 private boolean interactive;
106
107 @Component
108 private ArchetypeManager manager;
109
110
111
112
113 @Parameter( property = "archetype.filteredExtentions" )
114 private String archetypeFilteredExtentions;
115
116
117
118
119 @Parameter( property = "archetype.languages" )
120 private String archetypeLanguages;
121
122
123
124
125 @Parameter( defaultValue = "${user.home}/.m2/archetype.xml" )
126 private File archetypeRegistryFile;
127
128
129
130
131 @Parameter( property = "archetype.encoding", defaultValue = "UTF-8" )
132 private String defaultEncoding;
133
134
135
136
137 @Parameter( property = "archetype.partialArchetype" )
138 private boolean partialArchetype = false;
139
140
141
142
143
144 @Parameter( property = "archetype.preserveCData" )
145 private boolean preserveCData = false;
146
147 @Parameter( defaultValue = "${localRepository}", readonly = true )
148 private ArtifactRepository localRepository;
149
150
151
152
153
154 @Parameter( property = "archetype.keepParent" )
155 private boolean keepParent = true;
156
157
158
159
160 @Parameter( defaultValue = "${project}", readonly = true, required = true )
161 private MavenProject project;
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195 @Parameter( property = "archetype.properties" )
196 private File propertyFile;
197
198
199
200
201
202 @Parameter( property = "archetype.postPhase", defaultValue = "package" )
203 private String archetypePostPhase;
204
205
206
207
208 @Parameter( defaultValue = "${project.build.directory}/generated-sources/archetype" )
209 private File outputDirectory;
210
211 @Parameter( property = "testMode" )
212 private boolean testMode;
213
214
215
216
217
218 @Parameter( property = "packageName" )
219 private String packageName;
220
221 @Parameter( defaultValue = "${session}", readonly = true, required = true )
222 private MavenSession session;
223
224 public void execute()
225 throws MojoExecutionException, MojoFailureException
226 {
227 Properties executionProperties = session.getExecutionProperties();
228 try
229 {
230 if ( propertyFile != null )
231 {
232 propertyFile.getParentFile().mkdirs();
233 }
234
235 List<String> languages = getLanguages( archetypeLanguages, propertyFile );
236
237 Properties properties =
238 configurator.configureArchetypeCreation( project, Boolean.valueOf( interactive ), executionProperties,
239 propertyFile, languages );
240
241 List<String> filtereds = getFilteredExtensions( archetypeFilteredExtentions, propertyFile );
242
243 ArchetypeCreationRequest request =
244 new ArchetypeCreationRequest().setDefaultEncoding( defaultEncoding ).setProject( project )
245 .setProperties( properties ).setLanguages( languages )
246 .setFiltereds( filtereds )
247 .setPreserveCData( preserveCData ).setKeepParent(
248 keepParent ).setPartialArchetype( partialArchetype )
249 .setArchetypeRegistryFile(
250 archetypeRegistryFile ).setLocalRepository( localRepository )
251 .setPackageName( packageName ).setPostPhase(
252 archetypePostPhase ).setOutputDirectory( outputDirectory );
253
254 ArchetypeCreationResult result = manager.createArchetypeFromProject( request );
255
256 if ( result.getCause() != null )
257 {
258 throw new MojoFailureException( result.getCause(), result.getCause().getMessage(),
259 result.getCause().getMessage() );
260 }
261
262 getLog().info( "Archetype created in " + outputDirectory );
263
264 if ( testMode )
265 {
266
267
268
269
270
271
272
273
274
275 }
276
277 }
278 catch ( MojoFailureException ex )
279 {
280 throw ex;
281 }
282 catch ( Exception ex )
283 {
284 throw new MojoFailureException( ex, ex.getMessage(), ex.getMessage() );
285 }
286 }
287
288 private List<String> getFilteredExtensions( String archetypeFilteredExtentions, File propertyFile )
289 {
290 List<String> filteredExtensions = new ArrayList<String>();
291
292 if ( StringUtils.isNotEmpty( archetypeFilteredExtentions ) )
293 {
294 filteredExtensions.addAll( Arrays.asList( StringUtils.split( archetypeFilteredExtentions, "," ) ) );
295
296 getLog().debug( "Found in command line extensions = " + filteredExtensions );
297 }
298
299 if ( filteredExtensions.isEmpty() && propertyFile != null && propertyFile.exists() )
300 {
301 Properties properties = PropertyUtils.loadProperties( propertyFile );
302
303 String extensions = properties.getProperty( Constants.ARCHETYPE_FILTERED_EXTENSIONS );
304 if ( StringUtils.isNotEmpty( extensions ) )
305 {
306 filteredExtensions.addAll( Arrays.asList( StringUtils.split( extensions, "," ) ) );
307 }
308
309 getLog().debug( "Found in propertyFile " + propertyFile.getName() + " extensions = " + filteredExtensions );
310 }
311
312 if ( filteredExtensions.isEmpty() )
313 {
314 filteredExtensions.addAll( Constants.DEFAULT_FILTERED_EXTENSIONS );
315
316 getLog().debug( "Using default extensions = " + filteredExtensions );
317 }
318
319 return filteredExtensions;
320 }
321
322 private List<String> getLanguages( String archetypeLanguages, File propertyFile )
323 {
324 List<String> resultingLanguages = new ArrayList<String>();
325
326 if ( StringUtils.isNotEmpty( archetypeLanguages ) )
327 {
328 resultingLanguages.addAll( Arrays.asList( StringUtils.split( archetypeLanguages, "," ) ) );
329
330 getLog().debug( "Found in command line languages = " + resultingLanguages );
331 }
332
333 if ( resultingLanguages.isEmpty() && propertyFile != null && propertyFile.exists() )
334 {
335 Properties properties = PropertyUtils.loadProperties( propertyFile );
336
337 String languages = properties.getProperty( Constants.ARCHETYPE_LANGUAGES );
338 if ( StringUtils.isNotEmpty( languages ) )
339 {
340 resultingLanguages.addAll( Arrays.asList( StringUtils.split( languages, "," ) ) );
341 }
342
343 getLog().debug( "Found in propertyFile " + propertyFile.getName() + " languages = " + resultingLanguages );
344 }
345
346 if ( resultingLanguages.isEmpty() )
347 {
348 resultingLanguages.addAll( Constants.DEFAULT_LANGUAGES );
349
350 getLog().debug( "Using default languages = " + resultingLanguages );
351 }
352
353 return resultingLanguages;
354 }
355 }