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