1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.resources;
20
21 import java.io.File;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.LinkedHashSet;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Properties;
29
30 import org.apache.commons.lang3.StringUtils;
31 import org.apache.maven.execution.MavenSession;
32 import org.apache.maven.model.Resource;
33 import org.apache.maven.plugin.AbstractMojo;
34 import org.apache.maven.plugin.MojoExecutionException;
35 import org.apache.maven.plugins.annotations.Component;
36 import org.apache.maven.plugins.annotations.LifecyclePhase;
37 import org.apache.maven.plugins.annotations.Mojo;
38 import org.apache.maven.plugins.annotations.Parameter;
39 import org.apache.maven.project.MavenProject;
40 import org.apache.maven.shared.filtering.MavenFilteringException;
41 import org.apache.maven.shared.filtering.MavenResourcesExecution;
42 import org.apache.maven.shared.filtering.MavenResourcesFiltering;
43
44
45
46
47
48
49
50
51
52
53 @Mojo(name = "resources", defaultPhase = LifecyclePhase.PROCESS_RESOURCES, requiresProject = true, threadSafe = true)
54 public class ResourcesMojo extends AbstractMojo {
55
56
57
58
59 @Parameter(defaultValue = "${project.build.sourceEncoding}")
60 protected String encoding;
61
62
63
64
65
66
67
68 @Parameter
69 protected String propertiesEncoding;
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 @Parameter(defaultValue = "${project.build.filters}", readonly = true)
97 protected List<String> buildFilters;
98
99
100
101
102
103
104
105
106
107
108
109
110
111 @Parameter
112 protected List<String> filters;
113
114
115
116
117
118
119
120
121
122 @Parameter(defaultValue = "true")
123 protected boolean useBuildFilters;
124
125
126
127
128 @Component(role = MavenResourcesFiltering.class, hint = "default")
129 protected MavenResourcesFiltering mavenResourcesFiltering;
130
131
132
133
134 @Component(role = MavenResourcesFiltering.class)
135 protected Map<String, MavenResourcesFiltering> mavenResourcesFilteringMap;
136
137
138
139
140 @Parameter(defaultValue = "${session}", readonly = true, required = true)
141 protected MavenSession session;
142
143
144
145
146
147
148
149
150 @Parameter
151 protected String escapeString;
152
153
154
155
156
157
158 @Parameter(defaultValue = "false")
159 private boolean overwrite;
160
161
162
163
164
165
166 @Parameter(defaultValue = "false")
167 protected boolean includeEmptyDirs;
168
169
170
171
172
173
174 @Parameter
175 protected List<String> nonFilteredFileExtensions;
176
177
178
179
180
181
182 @Parameter(defaultValue = "true")
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
205
206 @Parameter
207 protected LinkedHashSet<String> delimiters;
208
209
210
211
212
213
214 @Parameter(defaultValue = "true")
215 protected boolean useDefaultDelimiters;
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244 @Parameter(defaultValue = "true")
245 protected boolean addDefaultExcludes;
246
247
248
249
250
251
252
253
254
255
256 @Parameter
257 private List<String> mavenFilteringHints;
258
259
260
261
262 private List<MavenResourcesFiltering> mavenFilteringComponents = new ArrayList<>();
263
264
265
266
267
268
269 @Parameter(defaultValue = "false")
270 private boolean supportMultiLineFiltering;
271
272
273
274
275
276
277 @Parameter(defaultValue = "false")
278 private boolean fileNameFiltering;
279
280
281
282
283
284
285
286 @Parameter(property = "maven.resources.skip", defaultValue = "false")
287 private boolean skip;
288
289
290
291
292 public void execute() throws MojoExecutionException {
293 if (isSkip()) {
294 getLog().info("Skipping the execution.");
295 return;
296 }
297
298 if (StringUtils.isBlank(encoding) && isFilteringEnabled(getResources())) {
299 getLog().warn("File encoding has not been set, using platform encoding "
300 + System.getProperty("file.encoding")
301 + ". Build is platform dependent!");
302 getLog().warn("See https://maven.apache.org/general.html#encoding-warning");
303 }
304
305 try {
306 List<String> combinedFilters = getCombinedFiltersList();
307
308 MavenResourcesExecution mavenResourcesExecution = new MavenResourcesExecution(
309 getResources(),
310 getOutputDirectory(),
311 project,
312 encoding,
313 combinedFilters,
314 Collections.emptyList(),
315 session);
316
317 mavenResourcesExecution.setEscapeWindowsPaths(escapeWindowsPaths);
318
319
320
321 mavenResourcesExecution.setInjectProjectBuildFilters(false);
322
323 mavenResourcesExecution.setEscapeString(escapeString);
324 mavenResourcesExecution.setOverwrite(overwrite);
325 mavenResourcesExecution.setIncludeEmptyDirs(includeEmptyDirs);
326 mavenResourcesExecution.setSupportMultiLineFiltering(supportMultiLineFiltering);
327 mavenResourcesExecution.setFilterFilenames(fileNameFiltering);
328 mavenResourcesExecution.setAddDefaultExcludes(addDefaultExcludes);
329
330
331 Properties additionalProperties = addSeveralSpecialProperties();
332 mavenResourcesExecution.setAdditionalProperties(additionalProperties);
333
334
335 mavenResourcesExecution.setDelimiters(delimiters, useDefaultDelimiters);
336
337
338 mavenResourcesExecution.setPropertiesEncoding(propertiesEncoding);
339
340 if (nonFilteredFileExtensions != null) {
341 mavenResourcesExecution.setNonFilteredFileExtensions(nonFilteredFileExtensions);
342 }
343 mavenResourcesFiltering.filterResources(mavenResourcesExecution);
344
345 executeUserFilterComponents(mavenResourcesExecution);
346 } catch (MavenFilteringException e) {
347 throw new MojoExecutionException(e.getMessage(), e);
348 }
349 }
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365 private Properties addSeveralSpecialProperties() {
366 String timeStamp = new MavenBuildTimestamp().formattedTimestamp();
367 Properties additionalProperties = new Properties();
368 additionalProperties.put("maven.build.timestamp", timeStamp);
369 if (project.getBasedir() != null) {
370 additionalProperties.put(
371 "project.baseUri",
372 project.getBasedir().getAbsoluteFile().toURI().toString());
373 }
374
375 return additionalProperties;
376 }
377
378
379
380
381
382
383
384 protected void executeUserFilterComponents(MavenResourcesExecution mavenResourcesExecution)
385 throws MojoExecutionException, MavenFilteringException {
386
387 if (mavenFilteringHints != null) {
388 for (String hint : mavenFilteringHints) {
389 MavenResourcesFiltering userFilterComponent = mavenResourcesFilteringMap.get(hint);
390 if (userFilterComponent != null) {
391 getLog().debug("added user filter component with hint: " + hint);
392 mavenFilteringComponents.add(userFilterComponent);
393 } else {
394 throw new MojoExecutionException(
395 "User filter with hint `" + hint + "` requested, but not present. Discovered filters are: "
396 + mavenResourcesFilteringMap.keySet());
397 }
398 }
399 } else {
400 getLog().debug("no user filter components");
401 }
402
403 if (mavenFilteringComponents != null && !mavenFilteringComponents.isEmpty()) {
404 getLog().debug("execute user filters");
405 for (MavenResourcesFiltering filter : mavenFilteringComponents) {
406 filter.filterResources(mavenResourcesExecution);
407 }
408 }
409 }
410
411
412
413
414 protected List<String> getCombinedFiltersList() {
415 if (filters == null || filters.isEmpty()) {
416 return useBuildFilters ? buildFilters : null;
417 } else {
418 List<String> result = new ArrayList<>();
419
420 if (useBuildFilters && buildFilters != null && !buildFilters.isEmpty()) {
421 result.addAll(buildFilters);
422 }
423
424 result.addAll(filters);
425
426 return result;
427 }
428 }
429
430
431
432
433
434
435
436 private boolean isFilteringEnabled(Collection<Resource> theResources) {
437 if (theResources != null) {
438 for (Resource resource : theResources) {
439 if (resource.isFiltering()) {
440 return true;
441 }
442 }
443 }
444 return false;
445 }
446
447
448
449
450 public List<Resource> getResources() {
451 return resources;
452 }
453
454
455
456
457 public void setResources(List<Resource> resources) {
458 this.resources = resources;
459 }
460
461
462
463
464 public File getOutputDirectory() {
465 return outputDirectory;
466 }
467
468
469
470
471 public void setOutputDirectory(File outputDirectory) {
472 this.outputDirectory = outputDirectory;
473 }
474
475
476
477
478 public boolean isOverwrite() {
479 return overwrite;
480 }
481
482
483
484
485 public void setOverwrite(boolean overwrite) {
486 this.overwrite = overwrite;
487 }
488
489
490
491
492 public boolean isIncludeEmptyDirs() {
493 return includeEmptyDirs;
494 }
495
496
497
498
499 public void setIncludeEmptyDirs(boolean includeEmptyDirs) {
500 this.includeEmptyDirs = includeEmptyDirs;
501 }
502
503
504
505
506 public List<String> getFilters() {
507 return filters;
508 }
509
510
511
512
513 public void setFilters(List<String> filters) {
514 this.filters = filters;
515 }
516
517
518
519
520 public LinkedHashSet<String> getDelimiters() {
521 return delimiters;
522 }
523
524
525
526
527 public void setDelimiters(LinkedHashSet<String> delimiters) {
528 this.delimiters = delimiters;
529 }
530
531
532
533
534 public boolean isUseDefaultDelimiters() {
535 return useDefaultDelimiters;
536 }
537
538
539
540
541 public void setUseDefaultDelimiters(boolean useDefaultDelimiters) {
542 this.useDefaultDelimiters = useDefaultDelimiters;
543 }
544
545
546
547
548 public boolean isSkip() {
549 return skip;
550 }
551 }