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.model.path;
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.util.ArrayList;
27  import java.util.List;
28  
29  import org.apache.maven.model.Build;
30  import org.apache.maven.model.Model;
31  import org.apache.maven.model.Reporting;
32  import org.apache.maven.model.Resource;
33  import org.apache.maven.model.building.ModelBuildingRequest;
34  
35  /**
36   * Resolves relative paths within a model against a specific base directory.
37   *
38   * @author Benjamin Bentmann
39   */
40  @Named
41  @Singleton
42  public class DefaultModelPathTranslator implements ModelPathTranslator {
43  
44      @Inject
45      private PathTranslator pathTranslator;
46  
47      public DefaultModelPathTranslator setPathTranslator(PathTranslator pathTranslator) {
48          this.pathTranslator = pathTranslator;
49          return this;
50      }
51  
52      @Override
53      public void alignToBaseDirectory(Model model, File basedir, ModelBuildingRequest request) {
54          if (model == null || basedir == null) {
55              return;
56          }
57  
58          Build build = model.getBuild();
59  
60          if (build != null) {
61              build.setDirectory(alignToBaseDirectory(build.getDirectory(), basedir));
62  
63              build.setSourceDirectory(alignToBaseDirectory(build.getSourceDirectory(), basedir));
64  
65              build.setTestSourceDirectory(alignToBaseDirectory(build.getTestSourceDirectory(), basedir));
66  
67              build.setScriptSourceDirectory(alignToBaseDirectory(build.getScriptSourceDirectory(), basedir));
68  
69              for (Resource resource : build.getResources()) {
70                  resource.setDirectory(alignToBaseDirectory(resource.getDirectory(), basedir));
71              }
72  
73              for (Resource resource : build.getTestResources()) {
74                  resource.setDirectory(alignToBaseDirectory(resource.getDirectory(), basedir));
75              }
76  
77              if (build.getFilters() != null) {
78                  List<String> filters = new ArrayList<>(build.getFilters().size());
79                  for (String filter : build.getFilters()) {
80                      filters.add(alignToBaseDirectory(filter, basedir));
81                  }
82                  build.setFilters(filters);
83              }
84  
85              build.setOutputDirectory(alignToBaseDirectory(build.getOutputDirectory(), basedir));
86  
87              build.setTestOutputDirectory(alignToBaseDirectory(build.getTestOutputDirectory(), basedir));
88          }
89  
90          Reporting reporting = model.getReporting();
91  
92          if (reporting != null) {
93              reporting.setOutputDirectory(alignToBaseDirectory(reporting.getOutputDirectory(), basedir));
94          }
95      }
96  
97      private String alignToBaseDirectory(String path, File basedir) {
98          return pathTranslator.alignToBaseDirectory(path, basedir);
99      }
100 }