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.IOException;
24  import java.util.List;
25  
26  import org.apache.maven.execution.MavenSession;
27  import org.apache.maven.project.MavenProject;
28  import org.apache.maven.shared.utils.io.FileUtils;
29  import org.apache.maven.shared.utils.io.FileUtils.FilterWrapper;
30  import org.codehaus.plexus.component.annotations.Component;
31  import org.codehaus.plexus.component.annotations.Requirement;
32  import org.sonatype.plexus.build.incremental.BuildContext;
33  
34  /**
35   * @author Olivier Lamy
36   */
37  @Component( role = org.apache.maven.shared.filtering.MavenFileFilter.class, hint = "default" )
38  public class DefaultMavenFileFilter
39      extends BaseFilter
40      implements MavenFileFilter
41  {
42  
43      @Requirement
44      private MavenReaderFilter readerFilter;
45  
46      @Requirement
47      private BuildContext buildContext;
48  
49      @Override
50      public void copyFile( File from, File to, boolean filtering, MavenProject mavenProject, List<String> filters,
51                            boolean escapedBackslashesInFilePath, String encoding, MavenSession mavenSession )
52                                throws MavenFilteringException
53      {
54          MavenResourcesExecution mre = new MavenResourcesExecution();
55          mre.setMavenProject( mavenProject );
56          mre.setFileFilters( filters );
57          mre.setEscapeWindowsPaths( escapedBackslashesInFilePath );
58          mre.setMavenSession( mavenSession );
59          mre.setInjectProjectBuildFilters( true );
60  
61          List<FileUtils.FilterWrapper> filterWrappers = getDefaultFilterWrappers( mre );
62          copyFile( from, to, filtering, filterWrappers, encoding );
63      }
64  
65      @Override
66      public void copyFile( MavenFileFilterRequest mavenFileFilterRequest )
67          throws MavenFilteringException
68      {
69          List<FilterWrapper> filterWrappers = getDefaultFilterWrappers( mavenFileFilterRequest );
70  
71          copyFile( mavenFileFilterRequest.getFrom(), mavenFileFilterRequest.getTo(),
72                    mavenFileFilterRequest.isFiltering(), filterWrappers, mavenFileFilterRequest.getEncoding() );
73      }
74  
75      @Override
76      public void copyFile( File from, File to, boolean filtering, List<FileUtils.FilterWrapper> filterWrappers,
77                            String encoding )
78                                throws MavenFilteringException
79      {
80          // overwrite forced to false to preserve backward comp
81          copyFile( from, to, filtering, filterWrappers, encoding, false );
82      }
83  
84      @Override
85      public void copyFile( File from, File to, boolean filtering, List<FileUtils.FilterWrapper> filterWrappers,
86                            String encoding, boolean overwrite )
87                                throws MavenFilteringException
88      {
89          try
90          {
91              if ( filtering )
92              {
93                  if ( getLogger().isDebugEnabled() )
94                  {
95                      getLogger().debug( "filtering " + from.getPath() + " to " + to.getPath() );
96                  }
97                  FilterWrapper[] array = filterWrappers.toArray( new FilterWrapper[0] );
98                  FileUtils.copyFile( from, to, encoding, array, false );
99              }
100             else
101             {
102                 if ( getLogger().isDebugEnabled() )
103                 {
104                     getLogger().debug( "copy " + from.getPath() + " to " + to.getPath() );
105                 }
106                 FileUtils.copyFile( from, to, encoding, new FileUtils.FilterWrapper[0], overwrite );
107             }
108 
109             buildContext.refresh( to );
110         }
111         catch ( IOException e )
112         {
113             throw new MavenFilteringException( e.getMessage(), e );
114         }
115     }
116 }