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