View Javadoc
1   package org.apache.maven.shared.filtering;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *    http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.Reader;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import org.apache.maven.execution.MavenSession;
28  import org.apache.maven.model.Resource;
29  import org.apache.maven.project.MavenProject;
30  import org.codehaus.plexus.interpolation.StringSearchInterpolator;
31  import org.codehaus.plexus.interpolation.ValueSource;
32  
33  /**
34   * A bean to configure a resources filtering execution.
35   *
36   * @author Olivier Lamy
37   */
38  public class MavenResourcesExecution
39      extends AbstractMavenFilteringRequest
40  {
41  
42      private List<Resource> resources;
43  
44      private File outputDirectory;
45  
46      private List<String> nonFilteredFileExtensions;
47  
48      private List<FilterWrapper> filterWrappers;
49  
50      private File resourcesBaseDirectory;
51  
52      private boolean useDefaultFilterWrappers = false;
53  
54      private boolean filterFilenames = false;
55  
56      private String encoding;
57  
58      /**
59       * @since 3.2.0
60       */
61      private String propertiesEncoding;
62  
63      /**
64       * By default files like {@code .gitignore}, {@code .cvsignore} etc. are excluded which means they will not being
65       * copied. If you need them for a particular reason you can do that by settings this to {@code false}. This means
66       * all files like the following will be copied.
67       * <ul>
68       * <li>Misc: &#42;&#42;/&#42;~, &#42;&#42;/#&#42;#, &#42;&#42;/.#&#42;, &#42;&#42;/%&#42;%, &#42;&#42;/._&#42;</li>
69       * <li>CVS: &#42;&#42;/CVS, &#42;&#42;/CVS/&#42;&#42;, &#42;&#42;/.cvsignore</li>
70       * <li>RCS: &#42;&#42;/RCS, &#42;&#42;/RCS/&#42;&#42;</li>
71       * <li>SCCS: &#42;&#42;/SCCS, &#42;&#42;/SCCS/&#42;&#42;</li>
72       * <li>VSSercer: &#42;&#42;/vssver.scc</li>
73       * <li>MKS: &#42;&#42;/project.pj</li>
74       * <li>SVN: &#42;&#42;/.svn, &#42;&#42;/.svn/&#42;&#42;</li>
75       * <li>GNU: &#42;&#42;/.arch-ids, &#42;&#42;/.arch-ids/&#42;&#42;</li>
76       * <li>Bazaar: &#42;&#42;/.bzr, &#42;&#42;/.bzr/&#42;&#42;</li>
77       * <li>SurroundSCM: &#42;&#42;/.MySCMServerInfo</li>
78       * <li>Mac: &#42;&#42;/.DS_Store</li>
79       * <li>Serena Dimension: &#42;&#42;/.metadata, &#42;&#42;/.metadata/&#42;&#42;</li>
80       * <li>Mercurial: &#42;&#42;/.hg, &#42;&#42;/.hg/&#42;&#42;, &#42;&#42;/.hgignore,</li>
81       * <li>GIT: &#42;&#42;/.git, &#42;&#42;/.gitignore, &#42;&#42;/.gitattributes, &#42;&#42;/.git/&#42;&#42;</li>
82       * <li>Bitkeeper: &#42;&#42;/BitKeeper, &#42;&#42;/BitKeeper/&#42;&#42;, &#42;&#42;/ChangeSet,
83       * &#42;&#42;/ChangeSet/&#42;&#42;</li>
84       * <li>Darcs: &#42;&#42;/_darcs, &#42;&#42;/_darcs/&#42;&#42;, &#42;&#42;/.darcsrepo,
85       * &#42;&#42;/.darcsrepo/&#42;&#42;&#42;&#42;/-darcs-backup&#42;, &#42;&#42;/.darcs-temp-mail
86       * </ul>
87       *
88       * @since 3.1.0
89       */
90      private boolean addDefaultExcludes = true;
91  
92      /**
93       * Overwrite existing files even if the destination files are newer. <code>false</code> by default.
94       *
95       * @since 1.0-beta-2
96       */
97      private boolean overwrite = false;
98  
99      /**
100      * Copy any empty directories included in the Resources.
101      *
102      * @since 1.0-beta-2
103      */
104     private boolean includeEmptyDirs = false;
105 
106     /**
107      * Do not stop trying to filter tokens when reaching EOL.
108      *
109      * @since 1.0
110      */
111     private boolean supportMultiLineFiltering;
112 
113     /**
114      * Write resources to a flattened directory structure.
115      *
116      */
117     private boolean flatten = false;
118 
119     /**
120      * Do nothing.
121      */
122     public MavenResourcesExecution()
123     {
124         // no op
125     }
126 
127     /**
128      * As we use a Maven project <code>useDefaultFilterWrappers</code> will be set to <code>true</code>. The
129      * {@code useDefaultExcludes} is set to {@code true}.
130      *
131      * @param resources The list of resources.
132      * @param outputDirectory The output directory.
133      * @param mavenProject The maven project.
134      * @param encoding The given encoding.
135      * @param fileFilters The file filters.
136      * @param nonFilteredFileExtensions The extensions which should not being filtered.
137      * @param mavenSession The maven session.
138      */
139     public MavenResourcesExecution( List<Resource> resources, File outputDirectory, MavenProject mavenProject,
140                                     String encoding, List<String> fileFilters, List<String> nonFilteredFileExtensions,
141                                     MavenSession mavenSession )
142     {
143         super( mavenProject, fileFilters, mavenSession );
144         this.encoding = encoding;
145         this.resources = resources;
146         this.outputDirectory = outputDirectory;
147         this.nonFilteredFileExtensions = nonFilteredFileExtensions;
148         this.useDefaultFilterWrappers = true;
149         this.addDefaultExcludes = true;
150         this.resourcesBaseDirectory = mavenProject.getBasedir();
151     }
152 
153     /**
154      * @param resources The list of resources.
155      * @param outputDirectory The output directory.
156      * @param encoding The given encoding.
157      * @param filterWrappers The list of filter wrappers.
158      * @param resourcesBaseDirectory The resources base directory.
159      * @param nonFilteredFileExtensions The list of extensions which should not being filtered.
160      */
161     public MavenResourcesExecution( List<Resource> resources, File outputDirectory, String encoding,
162                                     List<FilterWrapper> filterWrappers, File resourcesBaseDirectory,
163                                     List<String> nonFilteredFileExtensions )
164     {
165         this();
166         this.resources = resources;
167         this.outputDirectory = outputDirectory;
168         this.filterWrappers = filterWrappers;
169         this.nonFilteredFileExtensions = nonFilteredFileExtensions;
170         this.resourcesBaseDirectory = resourcesBaseDirectory;
171         this.useDefaultFilterWrappers = false;
172         setEncoding( encoding );
173     }
174 
175     /**
176      * Return the encoding.
177      *
178      * @return Current encoding.
179      */
180     public String getEncoding()
181     {
182         return encoding;
183     }
184 
185     /**
186      * Set the value for encoding.
187      *
188      * @param encoding Give the new value for encoding.
189      */
190     public void setEncoding( String encoding )
191     {
192         this.encoding = encoding;
193     }
194 
195     /**
196      * Return the encoding of properties files.
197      *
198      * @return Current encoding of properties files.
199      * @since 3.2.0
200      */
201     public String getPropertiesEncoding()
202     {
203         return propertiesEncoding;
204     }
205 
206     /**
207      * Set the value for encoding of properties files.
208      *
209      * @param propertiesEncoding Give the new value for encoding of properties files.
210      * @since 3.2.0
211      */
212     public void setPropertiesEncoding( String propertiesEncoding )
213     {
214         this.propertiesEncoding = propertiesEncoding;
215     }
216 
217     /**
218      * @return List of {@link org.apache.maven.model.Resource}
219      */
220     public List<Resource> getResources()
221     {
222         return resources;
223     }
224 
225     /**
226      * @param resources List of {@link org.apache.maven.model.Resource}
227      */
228     public void setResources( List<Resource> resources )
229     {
230         this.resources = resources;
231     }
232 
233     /**
234      * @return The output directory.
235      */
236     public File getOutputDirectory()
237     {
238         return outputDirectory;
239     }
240 
241     /**
242      * @param outputDirectory The output directory.
243      */
244     public void setOutputDirectory( File outputDirectory )
245     {
246         this.outputDirectory = outputDirectory;
247     }
248 
249     /**
250      * @return List of {@link String} file extensions not to filter
251      */
252     public List<String> getNonFilteredFileExtensions()
253     {
254         return nonFilteredFileExtensions;
255     }
256 
257     /**
258      * @param nonFilteredFileExtensions List of {@link String} file extensions to not filter
259      */
260     public void setNonFilteredFileExtensions( List<String> nonFilteredFileExtensions )
261     {
262         this.nonFilteredFileExtensions = nonFilteredFileExtensions;
263     }
264 
265     /**
266      * @return List of {@link FilterWrapper}
267      */
268     public List<FilterWrapper> getFilterWrappers()
269     {
270         return filterWrappers;
271     }
272 
273     /**
274      * @param filterWrappers List of {@link FilterWrapper}
275      */
276     public void setFilterWrappers( List<FilterWrapper> filterWrappers )
277     {
278         this.filterWrappers = filterWrappers;
279     }
280 
281     /**
282      * @param filterWrapper The filter wrapper which should be added.
283      */
284     public void addFilterWrapper( FilterWrapper filterWrapper )
285     {
286         if ( this.filterWrappers == null )
287         {
288             this.filterWrappers = new ArrayList<>();
289         }
290         this.filterWrappers.add( filterWrapper );
291     }
292 
293     /**
294      * @param valueSource {@link ValueSource}
295      * @param startExp start token like <code>${</code>
296      * @param endExp endToken <code>}</code>
297      * @param escapeString The escape string.
298      * @param multiLineFiltering do we support or use filtering on multi lines with start and endtoken on multi lines
299      * @since 1.0
300      */
301     public void addFilerWrapperWithEscaping( final ValueSource valueSource, final String startExp, final String endExp,
302                                              final String escapeString, final boolean multiLineFiltering )
303     {
304         addFilterWrapper( new FilterWrapper()
305         {
306             @Override
307             public Reader getReader( Reader reader )
308             {
309                 StringSearchInterpolator propertiesInterpolator = new StringSearchInterpolator( startExp, endExp );
310                 propertiesInterpolator.addValueSource( valueSource );
311                 propertiesInterpolator.setEscapeString( escapeString );
312                 InterpolatorFilterReaderLineEnding interpolatorFilterReader =
313                     new InterpolatorFilterReaderLineEnding( reader, propertiesInterpolator, startExp, endExp,
314                                                             multiLineFiltering );
315                 interpolatorFilterReader.setInterpolateWithPrefixPattern( false );
316                 return interpolatorFilterReader;
317             }
318         } );
319     }
320 
321     /**
322      * @return The resource base directory.
323      */
324     public File getResourcesBaseDirectory()
325     {
326         return resourcesBaseDirectory;
327     }
328 
329     /**
330      * @param resourcesBaseDirectory Set the resource base directory.
331      */
332     public void setResourcesBaseDirectory( File resourcesBaseDirectory )
333     {
334         this.resourcesBaseDirectory = resourcesBaseDirectory;
335     }
336 
337     /**
338      * @return use default filter wrapper
339      */
340     public boolean isUseDefaultFilterWrappers()
341     {
342         return useDefaultFilterWrappers;
343     }
344 
345     /**
346      * @param useDefaultFilterWrappers {@link #useDefaultFilterWrappers}
347      */
348     public void setUseDefaultFilterWrappers( boolean useDefaultFilterWrappers )
349     {
350         this.useDefaultFilterWrappers = useDefaultFilterWrappers;
351     }
352 
353     /**
354      * @return add the default excludes.
355      */
356     public boolean isAddDefaultExcludes()
357     {
358         return addDefaultExcludes;
359     }
360 
361     /**
362      * @param addDefaultExcludes {@link #addDefaultExcludes}
363      */
364     public void setAddDefaultExcludes( boolean addDefaultExcludes )
365     {
366         this.addDefaultExcludes = addDefaultExcludes;
367     }
368 
369     /**
370      * Overwrite existing files even if the destination files are newer.
371      *
372      * @return {@link #overwrite}
373      * @since 1.0-beta-2
374      */
375     public boolean isOverwrite()
376     {
377         return overwrite;
378     }
379 
380     /**
381      * Overwrite existing files even if the destination files are newer.
382      *
383      * @param overwrite overwrite true or false.
384      * @since 1.0-beta-2
385      */
386     public void setOverwrite( boolean overwrite )
387     {
388         this.overwrite = overwrite;
389     }
390 
391     /**
392      * Write to flattened directory structure.
393      *
394      * @return {@link #flatten}
395      */
396     public boolean isFlatten()
397     {
398         return flatten;
399     }
400 
401     /**
402      * Write to flattened directory structure.
403      *
404      * @param flatten flatten true or false.
405      */
406     public void setFlatten( boolean flatten )
407     {
408         this.flatten = flatten;
409     }
410 
411     /**
412      * Copy any empty directories included in the Resources.
413      *
414      * @return {@link #includeEmptyDirs}
415      * @since 1.0-beta-2
416      */
417     public boolean isIncludeEmptyDirs()
418     {
419         return includeEmptyDirs;
420     }
421 
422     /**
423      * Copy any empty directories included in the Resources.
424      *
425      * @param includeEmptyDirs {@code true} to include empty directories, otherwise {@code false}.
426      * @since 1.0-beta-2
427      */
428     public void setIncludeEmptyDirs( boolean includeEmptyDirs )
429     {
430         this.includeEmptyDirs = includeEmptyDirs;
431     }
432 
433     /**
434      * @return {@code true} if filenames are filtered, otherwise {@code false}
435      * @since 1.2
436      */
437     public boolean isFilterFilenames()
438     {
439         return filterFilenames;
440     }
441 
442     /**
443      * @param filterFilenames {@code true} if filenames should be filtered, otherwise {@code false}
444      * @since 1.2
445      */
446     public void setFilterFilenames( boolean filterFilenames )
447     {
448         this.filterFilenames = filterFilenames;
449     }
450 
451     /**
452      * @return {@link MavenResourcesExecution}
453      */
454     public MavenResourcesExecution copyOf()
455     {
456         MavenResourcesExecution mre = new MavenResourcesExecution();
457         mre.setAdditionalProperties( this.getAdditionalProperties() );
458         mre.setEncoding( this.getEncoding() );
459         mre.setEscapedBackslashesInFilePath( this.isEscapedBackslashesInFilePath() );
460         mre.setEscapeString( this.getEscapeString() );
461         mre.setFileFilters( copyList( this.getFileFilters() ) );
462         mre.setFilterWrappers( copyList( this.getFilterWrappers() ) );
463         mre.setIncludeEmptyDirs( this.isIncludeEmptyDirs() );
464         mre.setInjectProjectBuildFilters( this.isInjectProjectBuildFilters() );
465         mre.setMavenProject( this.getMavenProject() );
466         mre.setMavenSession( this.getMavenSession() );
467         mre.setNonFilteredFileExtensions( copyList( this.getNonFilteredFileExtensions() ) );
468         mre.setOutputDirectory( this.getOutputDirectory() );
469         mre.setOverwrite( this.isOverwrite() );
470         mre.setProjectStartExpressions( copyList( this.getProjectStartExpressions() ) );
471         mre.setResources( copyList( this.getResources() ) );
472         mre.setResourcesBaseDirectory( this.getResourcesBaseDirectory() );
473         mre.setUseDefaultFilterWrappers( this.isUseDefaultFilterWrappers() );
474         mre.setAddDefaultExcludes( this.isAddDefaultExcludes() );
475         mre.setSupportMultiLineFiltering( this.isSupportMultiLineFiltering() );
476         return mre;
477     }
478 
479     private <T> List<T> copyList( List<T> lst )
480     {
481         if ( lst == null )
482         {
483             return null;
484         }
485         else if ( lst.isEmpty() )
486         {
487             return new ArrayList<>();
488         }
489         else
490         {
491             return new ArrayList<>( lst );
492         }
493     }
494 
495     @Override
496     public boolean isSupportMultiLineFiltering()
497     {
498         return supportMultiLineFiltering;
499     }
500 
501     @Override
502     public void setSupportMultiLineFiltering( boolean supportMultiLineFiltering )
503     {
504         this.supportMultiLineFiltering = supportMultiLineFiltering;
505     }
506 }