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.nio.file.Path;
27  import java.util.ArrayList;
28  import java.util.List;
29  import java.util.Objects;
30  import java.util.function.Function;
31  
32  import org.apache.maven.api.model.Build;
33  import org.apache.maven.api.model.Model;
34  import org.apache.maven.api.model.Reporting;
35  import org.apache.maven.api.model.Resource;
36  import org.apache.maven.model.building.ModelBuildingRequest;
37  
38  /**
39   * Resolves relative paths within a model against a specific base directory.
40   *
41   */
42  @Named
43  @Singleton
44  public class DefaultModelPathTranslator implements ModelPathTranslator {
45  
46      private final PathTranslator pathTranslator;
47  
48      @Inject
49      public DefaultModelPathTranslator(PathTranslator pathTranslator) {
50          this.pathTranslator = pathTranslator;
51      }
52  
53      @Deprecated
54      @Override
55      public void alignToBaseDirectory(org.apache.maven.model.Model modelV3, File basedir, ModelBuildingRequest request) {
56          if (modelV3 == null || basedir == null) {
57              return;
58          }
59          alignToBaseDirectory(modelV3, basedir.toPath(), request);
60      }
61  
62      @Override
63      public void alignToBaseDirectory(org.apache.maven.model.Model modelV3, Path basedir, ModelBuildingRequest request) {
64          if (modelV3 == null || basedir == null) {
65              return;
66          }
67  
68          Model model = modelV3.getDelegate();
69          Build build = model.getBuild();
70          Build newBuild = null;
71          if (build != null) {
72              newBuild = Build.newBuilder(build)
73                      .directory(alignToBaseDirectory(build.getDirectory(), basedir))
74                      .sourceDirectory(alignToBaseDirectory(build.getSourceDirectory(), basedir))
75                      .testSourceDirectory(alignToBaseDirectory(build.getTestSourceDirectory(), basedir))
76                      .scriptSourceDirectory(alignToBaseDirectory(build.getScriptSourceDirectory(), basedir))
77                      .resources(map(build.getResources(), r -> alignToBaseDirectory(r, basedir)))
78                      .testResources(map(build.getTestResources(), r -> alignToBaseDirectory(r, basedir)))
79                      .filters(map(build.getFilters(), s -> alignToBaseDirectory(s, basedir)))
80                      .outputDirectory(alignToBaseDirectory(build.getOutputDirectory(), basedir))
81                      .testOutputDirectory(alignToBaseDirectory(build.getTestOutputDirectory(), basedir))
82                      .build();
83          }
84  
85          Reporting reporting = model.getReporting();
86          Reporting newReporting = null;
87          if (reporting != null) {
88              newReporting = Reporting.newBuilder(reporting)
89                      .outputDirectory(alignToBaseDirectory(reporting.getOutputDirectory(), basedir))
90                      .build();
91          }
92          if (newBuild != build || newReporting != reporting) {
93              modelV3.update(Model.newBuilder(model)
94                      .build(newBuild)
95                      .reporting(newReporting)
96                      .build());
97          }
98      }
99  
100     private <T> List<T> map(List<T> resources, Function<T, T> mapper) {
101         List<T> newResources = null;
102         if (resources != null) {
103             for (int i = 0; i < resources.size(); i++) {
104                 T resource = resources.get(i);
105                 T newResource = mapper.apply(resource);
106                 if (newResource != null) {
107                     if (newResources == null) {
108                         newResources = new ArrayList<>(resources);
109                     }
110                     newResources.set(i, newResource);
111                 }
112             }
113         }
114         return newResources;
115     }
116 
117     private Resource alignToBaseDirectory(Resource resource, Path basedir) {
118         if (resource != null) {
119             String newDir = alignToBaseDirectory(resource.getDirectory(), basedir);
120             if (newDir != null) {
121                 return resource.withDirectory(newDir);
122             }
123         }
124         return resource;
125     }
126 
127     private String alignToBaseDirectory(String path, Path basedir) {
128         String newPath = pathTranslator.alignToBaseDirectory(path, basedir);
129         return Objects.equals(path, newPath) ? null : newPath;
130     }
131 }