View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.shared.filtering;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import java.io.File;
26  import java.io.IOException;
27  import java.util.List;
28  
29  import org.apache.maven.execution.MavenSession;
30  import org.apache.maven.project.MavenProject;
31  import org.sonatype.plexus.build.incremental.BuildContext;
32  
33  import static java.util.Objects.requireNonNull;
34  
35  /**
36   * @author Olivier Lamy
37   */
38  @Singleton
39  @Named
40  public class DefaultMavenFileFilter extends BaseFilter implements MavenFileFilter {
41      private final BuildContext buildContext;
42  
43      @Inject
44      public DefaultMavenFileFilter(BuildContext buildContext) {
45          this.buildContext = requireNonNull(buildContext);
46      }
47  
48      @Override
49      public void copyFile(
50              File from,
51              File to,
52              boolean filtering,
53              MavenProject mavenProject,
54              List<String> filters,
55              boolean escapedBackslashesInFilePath,
56              String encoding,
57              MavenSession mavenSession)
58              throws MavenFilteringException {
59          MavenResourcesExecution mre = new MavenResourcesExecution();
60          mre.setMavenProject(mavenProject);
61          mre.setFileFilters(filters);
62          mre.setEscapeWindowsPaths(escapedBackslashesInFilePath);
63          mre.setMavenSession(mavenSession);
64          mre.setInjectProjectBuildFilters(true);
65  
66          List<FilterWrapper> filterWrappers = getDefaultFilterWrappers(mre);
67          copyFile(from, to, filtering, filterWrappers, encoding);
68      }
69  
70      @Override
71      public void copyFile(MavenFileFilterRequest mavenFileFilterRequest) throws MavenFilteringException {
72          List<FilterWrapper> filterWrappers = getDefaultFilterWrappers(mavenFileFilterRequest);
73  
74          copyFile(
75                  mavenFileFilterRequest.getFrom(),
76                  mavenFileFilterRequest.getTo(),
77                  mavenFileFilterRequest.isFiltering(),
78                  filterWrappers,
79                  mavenFileFilterRequest.getEncoding());
80      }
81  
82      @Override
83      public void copyFile(File from, File to, boolean filtering, List<FilterWrapper> filterWrappers, String encoding)
84              throws MavenFilteringException {
85          try {
86              if (filtering) {
87                  if (getLogger().isDebugEnabled()) {
88                      getLogger().debug("filtering " + from.getPath() + " to " + to.getPath());
89                  }
90                  FilterWrapper[] array = filterWrappers.toArray(new FilterWrapper[0]);
91                  FilteringUtils.copyFile(from, to, encoding, array, false);
92              } else {
93                  if (getLogger().isDebugEnabled()) {
94                      getLogger().debug("copy " + from.getPath() + " to " + to.getPath());
95                  }
96                  FilteringUtils.copyFile(from, to, encoding, new FilterWrapper[0], false);
97              }
98  
99              buildContext.refresh(to);
100         } catch (IOException e) {
101             throw new MavenFilteringException(
102                     (filtering ? "filtering " : "copying ") + from.getPath() + " to " + to.getPath() + " failed with "
103                             + e.getClass().getSimpleName(),
104                     e);
105         }
106     }
107 
108     @Override
109     @Deprecated
110     public void copyFile(
111             File from,
112             File to,
113             boolean filtering,
114             List<FilterWrapper> filterWrappers,
115             String encoding,
116             boolean overwrite)
117             throws MavenFilteringException {
118         // overwrite forced to false to preserve backward comp
119         copyFile(from, to, filtering, filterWrappers, encoding);
120     }
121 }