View Javadoc
1   /*
2    * Copyright 1014-2016 Polago AB.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  /*
18   * Licensed to the Apache Software Foundation (ASF) under one
19   * or more contributor license agreements.  See the NOTICE file
20   * distributed with this work for additional information
21   * regarding copyright ownership.  The ASF licenses this file
22   * to you under the Apache License, Version 2.0 (the
23   * "License"); you may not use this file except in compliance
24   * with the License.  You may obtain a copy of the License at
25   *
26   *   http://www.apache.org/licenses/LICENSE-2.0
27   *
28   * Unless required by applicable law or agreed to in writing,
29   * software distributed under the License is distributed on an
30   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
31   * KIND, either express or implied.  See the License for the
32   * specific language governing permissions and limitations
33   * under the License.
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   * Merges a set of properties files into an output file.
69   */
70  @Mojo(name = "merge", defaultPhase = LifecyclePhase.PROCESS_RESOURCES, requiresProject = true, threadSafe = true)
71  public class MergePropertiesMojo extends AbstractMojo implements Contextualizable {
72  
73      /**
74       * The Maven Project to use.
75       */
76      @Parameter(defaultValue = "${project}", required = true, readonly = true)
77      private MavenProject project;
78  
79      /**
80       * The MavenSession instance to use.
81       */
82      @Parameter(defaultValue = "${session}", required = true, readonly = true)
83      private MavenSession session;
84  
85      /**
86       * The MavenResourcesFiltering instance to use.
87       */
88      @Component(role = MavenResourcesFiltering.class, hint = "merge")
89      private MergeProperitesMavenResourcesFiltering mavenResourcesFiltering;
90  
91      /**
92       * The PlexusContainer instance to use.
93       */
94      private PlexusContainer plexusContainer;
95  
96      /**
97       * The output directory into which to create the outputFile.
98       */
99      @Parameter(required = true)
100     private File outputDirectory;
101 
102     /**
103      * The output filename that the properties should be merged into relative to the outputDirectory.
104      */
105     @Parameter(required = true)
106     private String outputFile;
107 
108     /**
109      * The list of resources to merge. Please see the <a href="http://maven.apache.org/pom.html#Resources">POM
110      * Reference</a> for a description of how to specify the resources element. Note that the
111      * <code>&lt;targetPath&gt;</code> element is always ignored and the default <code>&lt;include&gt;</code> pattern is
112      * <code>**&#47;*.properties</code>.
113      */
114     @Parameter(required = true)
115     private List<Resource> resources;
116 
117     /**
118      * The character encoding scheme to use. Note that Java Properties files are always encoded in ISO-8859-1.
119      */
120     @Parameter(property = "encoding", defaultValue = "ISO-8859-1")
121     private String encoding;
122 
123     /**
124      * The list of additional filter properties files to be used along with System and project properties.
125      * <p>
126      * See also: {@link #filters}.
127      */
128     @Parameter(defaultValue = "${project.build.filters}", readonly = true)
129     private List<String> buildFilters;
130 
131     /**
132      * The list of extra filter properties files to be used along with System properties, project properties, and filter
133      * properties files specified in the POM build/filters section, which should be used for the filtering during the
134      * current mojo execution.
135      */
136     @Parameter
137     private List<String> filters;
138 
139     /**
140      * If false, don't use the filters specified in the build/filters section of the POM when processing resources in
141      * this mojo execution.
142      * <p>
143      * See also: {@link #buildFilters} and {@link #filters}
144      */
145     @Parameter(defaultValue = "true")
146     private boolean useBuildFilters;
147 
148     /**
149      * Expression preceded with this String won't be interpolated \${foo} will be replaced with ${foo}.
150      */
151     @Parameter(property = "maven.resources.escapeString")
152     private String escapeString;
153 
154     /**
155      * Overwrite any existing outputFile even if the outputFile file is newer.
156      */
157     @Parameter(property = "maven.resources.overwrite", defaultValue = "false")
158     private boolean overwrite;
159 
160     /**
161      * Overwrite any duplicate properties instead of failing the build.
162      */
163     @Parameter(property = "maven.resources.overwrite", defaultValue = "false")
164     private boolean overwriteProperties;
165 
166     /**
167      * Whether to escape backslashes and colons in windows-style paths.
168      */
169     @Parameter(property = "maven.resources.escapeWindowsPaths", defaultValue = "true")
170     private boolean escapeWindowsPaths;
171 
172     /**
173      * <p>
174      * Set of delimiters for expressions to filter within the resources. These delimiters are specified in the form
175      * 'beginToken*endToken'. If no '*' is given, the delimiter is assumed to be the same for start and end.
176      * </p>
177      * <p>
178      * So, the default filtering delimiters might be specified as:
179      * </p>
180      *
181      * <pre>
182      * &lt;delimiters&gt;
183      *   &lt;delimiter&gt;${*}&lt;/delimiter&gt;
184      *   &lt;delimiter&gt;@&lt;/delimiter&gt;
185      * &lt;/delimiters&gt;
186      * </pre>
187      * <p>
188      * Since the '@' delimiter is the same on both ends, we don't need to specify '@*@' (though we can).
189      * </p>
190      */
191     @Parameter
192     private LinkedHashSet<String> delimiters;
193 
194     /**
195      * If false, don't use the maven's built-in delimiters.
196      */
197     @Parameter(defaultValue = "true")
198     private boolean useDefaultDelimiters;
199 
200     /**
201      * <p>
202      * List of plexus components hints which implements
203      * {@link MavenResourcesFiltering#filterResources(MavenResourcesExecution)} . They will be executed after the
204      * resources copying/filtering.
205      * </p>
206      */
207     @Parameter
208     private List<String> mavenFilteringHints;
209 
210     /**
211      * The list of user filtering components to use.
212      */
213     private final List<MavenResourcesFiltering> mavenFilteringComponents = new ArrayList<MavenResourcesFiltering>();
214 
215     /**
216      * Stop searching endToken at the end of line.
217      */
218     @Parameter(property = "maven.resources.supportMultiLineFiltering", defaultValue = "false")
219     private boolean supportMultiLineFiltering;
220 
221     /**
222      * Skip the execution of the plugin if you need to.
223      *
224      * @since 1.1
225      */
226     @Parameter(property = "maven.resources.skip", defaultValue = "false")
227     private boolean skip;
228 
229     /**
230      * {@inheritDoc}
231      */
232     @Override
233     public void contextualize(Context context) throws ContextException {
234         plexusContainer = (PlexusContainer) context.get(PlexusConstants.PLEXUS_KEY);
235     }
236 
237     /**
238      * {@inheritDoc}
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             // never include project build filters in this call, since we've
265             // already accounted for the POM build filters
266             // above, in getCombinedFiltersList().
267             mavenResourcesExecution.setInjectProjectBuildFilters(false);
268 
269             mavenResourcesExecution.setEscapeString(escapeString);
270             mavenResourcesExecution.setOverwrite(overwrite);
271             mavenResourcesExecution.setIncludeEmptyDirs(false);
272             mavenResourcesExecution.setSupportMultiLineFiltering(supportMultiLineFiltering);
273 
274             // Handle subject of MRESOURCES-99
275             Properties additionalProperties = addSeveralSpecialProperties();
276             mavenResourcesExecution.setAdditionalProperties(additionalProperties);
277 
278             // if these are NOT set, just use the defaults, which are '${*}' and '@'.
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      * This solves https://issues.apache.org/jira/browse/MRESOURCES-99.<br/>
291      * BUT:<br/>
292      * This should be done different than defining those properties a second time, cause they have already being defined
293      * in Maven Model Builder (package org.apache.maven.model.interpolation) via BuildTimestampValueSource. But those
294      * can't be found in the context which can be got from the maven core.<br/>
295      * A solution could be to put those values into the context by Maven core so they are accessible everywhere. (I'm
296      * not sure if this is a good idea). Better ideas are always welcome. The problem at the moment is that maven core
297      * handles usage of properties and replacements in the model, but does not the resource filtering which needed some
298      * of the properties.
299      *
300      * @return the new instance with those properties.
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      * Execute any user filters.
315      *
316      * @param mavenResourcesExecution the MavenResourcesExecution to use
317      * @throws MojoExecutionException indicating build error
318      * @throws MavenFilteringException indicating filtering problem
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      * Gets the combined list of all filters to use.
346      *
347      * @return a List of all filters to use
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      * Determines whether filtering has been enabled for any resource.
367      *
368      * @param targets The set of resources to check for filtering, may be <code>null</code>.
369      * @return <code>true</code> if at least one resource uses filtering, <code>false</code> otherwise.
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      * Gets the Resources to use.
384      *
385      * @return a list of Resources
386      */
387     public List<Resource> getResources() {
388         return resources;
389     }
390 
391     /**
392      * Gets the outputDirectory to use.
393      *
394      * @return the outputDirectory for this Mojo
395      */
396     public File getOutputDirectory() {
397         return outputDirectory;
398     }
399 
400     /**
401      * Gets the mavenResourcesFiltering property value.
402      *
403      * @return the current value of the mavenResourcesFiltering property
404      */
405     public MergeProperitesMavenResourcesFiltering getMavenResourcesFiltering() {
406         return mavenResourcesFiltering;
407     }
408 
409     /**
410      * Sets the mavenResourcesFiltering property.
411      *
412      * @param mavenResourcesFiltering the new property value
413      */
414     public void setMavenResourcesFiltering(MergeProperitesMavenResourcesFiltering mavenResourcesFiltering) {
415         this.mavenResourcesFiltering = mavenResourcesFiltering;
416     }
417 
418     /**
419      * Sets the project property.
420      *
421      * @param project the new property value
422      */
423     public void setProject(MavenProject project) {
424         this.project = project;
425     }
426 
427     /**
428      * Sets the overwriteProperties property.
429      *
430      * @param overwriteProperties the new property value
431      */
432     public void setOverwriteProperties(boolean overwriteProperties) {
433         this.overwriteProperties = overwriteProperties;
434     }
435 
436     /**
437      * Gets the skip property value.
438      *
439      * @return the current value of the skip property
440      */
441     public boolean isSkip() {
442         return skip;
443     }
444 
445 }