1 package org.apache.maven.plugin.resources;
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.execution.MavenSession;
23 import org.apache.maven.model.Resource;
24 import org.apache.maven.plugin.AbstractMojo;
25 import org.apache.maven.plugin.MojoExecutionException;
26 import org.apache.maven.project.MavenProject;
27 import org.apache.maven.shared.filtering.MavenFilteringException;
28 import org.apache.maven.shared.filtering.MavenResourcesExecution;
29 import org.apache.maven.shared.filtering.MavenResourcesFiltering;
30 import org.codehaus.plexus.PlexusConstants;
31 import org.codehaus.plexus.PlexusContainer;
32 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
33 import org.codehaus.plexus.context.Context;
34 import org.codehaus.plexus.context.ContextException;
35 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
36 import org.codehaus.plexus.util.ReaderFactory;
37 import org.codehaus.plexus.util.StringUtils;
38
39 import java.io.File;
40 import java.util.ArrayList;
41 import java.util.Collection;
42 import java.util.Collections;
43 import java.util.Iterator;
44 import java.util.LinkedHashSet;
45 import java.util.List;
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61 public class ResourcesMojo
62 extends AbstractMojo
63 implements Contextualizable
64 {
65
66
67
68
69
70
71 protected String encoding;
72
73
74
75
76
77
78
79 private File outputDirectory;
80
81
82
83
84
85
86
87
88 private List resources;
89
90
91
92
93
94
95 protected MavenProject project;
96
97
98
99
100
101
102
103
104
105
106
107 protected List buildFilters;
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123 protected List filters;
124
125
126
127
128
129
130
131
132
133 protected boolean useBuildFilters;
134
135
136
137
138
139
140 protected MavenResourcesFiltering mavenResourcesFiltering;
141
142
143
144
145
146
147 protected MavenSession session;
148
149
150
151
152
153
154
155 protected String escapeString;
156
157
158
159
160
161
162 private boolean overwrite;
163
164
165
166
167
168
169 protected boolean includeEmptyDirs;
170
171
172
173
174
175
176 protected List nonFilteredFileExtensions;
177
178
179
180
181
182
183 protected boolean escapeWindowsPaths;
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204 protected List delimiters;
205
206
207
208
209
210 protected boolean useDefaultDelimiters;
211
212
213
214
215
216
217
218
219
220 private List mavenFilteringHints;
221
222
223
224
225 private PlexusContainer plexusContainer;
226
227
228
229
230 private List mavenFilteringComponents = new ArrayList();
231
232
233
234
235
236
237 private boolean supportMultiLineFiltering;
238
239 public void contextualize( Context context )
240 throws ContextException
241 {
242 getLog().debug( "execute contextualize" );
243 plexusContainer = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
244 }
245
246 public void execute()
247 throws MojoExecutionException
248 {
249 try
250 {
251
252 if ( StringUtils.isEmpty( encoding ) && isFilteringEnabled( getResources() ) )
253 {
254 getLog().warn(
255 "File encoding has not been set, using platform encoding " + ReaderFactory.FILE_ENCODING
256 + ", i.e. build is platform dependent!" );
257 }
258
259 List filters = getCombinedFiltersList();
260
261 MavenResourcesExecution mavenResourcesExecution = new MavenResourcesExecution( getResources(),
262 getOutputDirectory(),
263 project, encoding, filters,
264 Collections.EMPTY_LIST,
265 session );
266
267 mavenResourcesExecution.setEscapeWindowsPaths( escapeWindowsPaths );
268
269
270
271 mavenResourcesExecution.setInjectProjectBuildFilters( false );
272
273 mavenResourcesExecution.setEscapeString( escapeString );
274 mavenResourcesExecution.setOverwrite( overwrite );
275 mavenResourcesExecution.setIncludeEmptyDirs( includeEmptyDirs );
276 mavenResourcesExecution.setSupportMultiLineFiltering( supportMultiLineFiltering );
277
278
279
280
281 if ( delimiters != null && !delimiters.isEmpty() )
282 {
283 LinkedHashSet delims = new LinkedHashSet();
284 if ( useDefaultDelimiters )
285 {
286 delims.addAll( mavenResourcesExecution.getDelimiters() );
287 }
288
289 for ( Iterator dIt = delimiters.iterator(); dIt.hasNext(); )
290 {
291 String delim = (String) dIt.next();
292 if ( delim == null )
293 {
294
295 delims.add( "${*}" );
296 }
297 else
298 {
299 delims.add( delim );
300 }
301 }
302
303 mavenResourcesExecution.setDelimiters( delims );
304 }
305
306 if ( nonFilteredFileExtensions != null )
307 {
308 mavenResourcesExecution.setNonFilteredFileExtensions( nonFilteredFileExtensions );
309 }
310 mavenResourcesFiltering.filterResources( mavenResourcesExecution );
311
312 executeUserFilterComponents( mavenResourcesExecution );
313 }
314 catch ( MavenFilteringException e )
315 {
316 throw new MojoExecutionException( e.getMessage(), e );
317 }
318 }
319
320
321
322
323 protected void executeUserFilterComponents( MavenResourcesExecution mavenResourcesExecution )
324 throws MojoExecutionException, MavenFilteringException
325 {
326
327 if ( mavenFilteringHints != null )
328 {
329 for ( Iterator ite = mavenFilteringHints.iterator(); ite.hasNext(); )
330 {
331 String hint = (String) ite.next();
332 try
333 {
334 mavenFilteringComponents
335 .add( plexusContainer.lookup( MavenResourcesFiltering.class.getName(), hint ) );
336 }
337 catch ( ComponentLookupException e )
338 {
339 throw new MojoExecutionException( e.getMessage(), e );
340 }
341 }
342 }
343 else
344 {
345 getLog().debug( "no use filter components" );
346 }
347
348 if ( mavenFilteringComponents != null && !mavenFilteringComponents.isEmpty() )
349 {
350 getLog().debug( "execute user filters" );
351 for ( Iterator ite = mavenFilteringComponents.iterator(); ite.hasNext(); )
352 {
353 MavenResourcesFiltering filter = (MavenResourcesFiltering) ite.next();
354 filter.filterResources( mavenResourcesExecution );
355 }
356 }
357 }
358
359 protected List getCombinedFiltersList()
360 {
361 if ( filters == null || filters.isEmpty() )
362 {
363 return useBuildFilters ? buildFilters : null;
364 }
365 else
366 {
367 List result = new ArrayList();
368
369 if ( useBuildFilters && buildFilters != null && !buildFilters.isEmpty() )
370 {
371 result.addAll( buildFilters );
372 }
373
374 result.addAll( filters );
375
376 return result;
377 }
378 }
379
380
381
382
383
384
385
386 private boolean isFilteringEnabled( Collection resources )
387 {
388 if ( resources != null )
389 {
390 for ( Iterator i = resources.iterator(); i.hasNext(); )
391 {
392 Resource resource = (Resource) i.next();
393 if ( resource.isFiltering() )
394 {
395 return true;
396 }
397 }
398 }
399 return false;
400 }
401
402 public List getResources()
403 {
404 return resources;
405 }
406
407 public void setResources( List resources )
408 {
409 this.resources = resources;
410 }
411
412 public File getOutputDirectory()
413 {
414 return outputDirectory;
415 }
416
417 public void setOutputDirectory( File outputDirectory )
418 {
419 this.outputDirectory = outputDirectory;
420 }
421
422 public boolean isOverwrite()
423 {
424 return overwrite;
425 }
426
427 public void setOverwrite( boolean overwrite )
428 {
429 this.overwrite = overwrite;
430 }
431
432 public boolean isIncludeEmptyDirs()
433 {
434 return includeEmptyDirs;
435 }
436
437 public void setIncludeEmptyDirs( boolean includeEmptyDirs )
438 {
439 this.includeEmptyDirs = includeEmptyDirs;
440 }
441
442 public List getFilters()
443 {
444 return filters;
445 }
446
447 public void setFilters( List filters )
448 {
449 this.filters = filters;
450 }
451
452 public List getDelimiters()
453 {
454 return delimiters;
455 }
456
457 public void setDelimiters( List delimiters )
458 {
459 this.delimiters = delimiters;
460 }
461
462 public boolean isUseDefaultDelimiters()
463 {
464 return useDefaultDelimiters;
465 }
466
467 public void setUseDefaultDelimiters( boolean useDefaultDelimiters )
468 {
469 this.useDefaultDelimiters = useDefaultDelimiters;
470 }
471
472 }