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