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