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